问题描述
我一直试图使用Ajax在Django应用程序中发布相关模型中的记录.为了更新一对父/子模型,我使用以下视图,并且将记录保存在各个模型中.但是,我不断收到以下错误消息:
I have been trying to post records in related models in my Django app using Ajax. In order to update a pair of parent/child models, I am using the following view and the records are getting saved in the respective models. However, I keep getting the following error:
以下是设置:
class MatListCreateView(LoginRequiredMixin, CreateView):
template_name = "..."
model = MatHdrList
form_class = CreateMatHdrListForm
def get(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
mat_bom_list = CreateBomMatListFormset
return self.render_to_response(
self.get_context_data(form=form, mat_bom_list=mat_bom_list)
)
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
mat_bom_list = CreateBomMatListFormset(self.request.POST)
if self.request.is_ajax and self.request.method == "POST":
if form.is_valid() and mat_bom_list.is_valid():
form.instance.created_by = self.request.user
self.object = form.save()
mat_bom_list.instance = self.object
mat_bom_list.save()
ser_instance = serializers.serialize('json', [ form, mat_bom_list, ])
return JsonResponse({"instance": ser_instance}, status=200)
else:
return JsonResponse({"error": form.errors}, status=400)
return JsonResponse({"error": "Whoops"}, status=400)
模板(jQuery ajax部分)
$('#materialListForm').submit(function(e) {
e.preventDefault();
var serializedData = $(this).serialize();
console.log(serializedData);
// var url1 = "{% url 'matl_list' %}";
$.ajax({
url: "{% url 'material_list_create' %}",
type: 'POST',
data: serializedData,
success: function() {
console.log('Data Saved');
// window.location = url1;
},
error: function (response, status, error) {
console.log('Problem encountered');
alert(response.responseText);
}
});
});
在发布(表单提交)警报时,出现 AttributeError警报:'ModelFormOptions'对象没有属性'concrete_model'
.退出警报不会退出页面(即使在ajax调用中使用 window.location
)也不会退出页面.返回到调用页面(即对象 listview ),发现记录已添加到父模型和子模型中.
On posting (form submit) alert to the effect AttributeError: 'ModelFormOptions' object has no attribute 'concrete_model'
is displayed. Dismissing the alert does not exit the page (even with window.location
in ajax call). Going back to the calling page (i.e. object listview) reveals that the records are added to both the parent and child models.
有人可以提出一种消除上述错误的方法吗?
推荐答案
如果您的班级中没有属性,则会发生属性错误.
Attribute error will occur if there is no attribute in your Class.
考虑我有一个这样的课程,
Consider I have a class like this,
Class SuperHero(Models.Model):
real_name = models.CharField(max_length=50)
character_name = models.CharField(max_length=50)
我还创建了一个实例
batman = SuperHero(real_name='BruceWayne'. character_name='BatMan')
现在,实名和字符名是此 SuperHero
对象的属性.
Now, real_name and character_name are the attributes for this SuperHero
object.
如果您访问的是类似 batman.real_name
的文件,它将为您提供 BruceWayne
的值.但是,如果您还需要 batman.super_powers
之类的东西,它将通过AttributeError:'SuperHero'对象没有属性'super_power'.
If you are accessing some like batman.real_name
, it will give you the value as BruceWayne
.But If you want some other things like batman.super_powers
, it will through `AttributeError: 'SuperHero' object has no attribute 'super_power'.
检查您的 ModelFormOptions
模型,希望 concrete_model
不存在.
Check your ModelFormOptions
model, I hope concrete_model
shouldn't be there.
这篇关于如何避免"AttributeError:'ModelFormOptions'对象没有属性'concrete_model'";在Django Ajax表单中提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!