我试图覆盖表单保存方法以返回字典。
forms.py
def save(self, commit=True):
mobj = {}
instance = super(_ObjectForm, self).save(commit=False)
mobj['par_prefix'] = "string"
if commit:
instance.save()
mobj['instance'] = instance
views.py
if form.is_valid():
new_object = form.save()
code...
rfmodel =inlineformset_factory(dmodel,ModelsField.related_model,form=get_model_form(ModelsField.related_model._meta.object_name,model))
prfmodel = rfmodel(request.POST,prefix=ModelsField.name,instance=new_object['instance'])
if prfmodel.is_valid():
new_object2 = prfmodel.save()
但是当表单提交时,我收到错误
AttributeError at /inventory/InsertModelPageForm/Product
'dict' object has no attribute 'product_id'
Request Method: POST
Request URL: http://127.0.0.1:8000/inventory/InsertModelPageForm/Product
Django Version: 1.9
Exception Type: AttributeError
Exception Value:
'dict' object has no attribute 'product_id'
Exception Location: /usr/local/lib/python2.7/dist-packages/django/forms/models.py in save_new, line 911
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/home/teo/MYWORKSPACE/dev/Lemix',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
Server time: Wed, 22 Jun 2016 06:36:54 +0000
>/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response
response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py in _wrapped_view
return view_func(request, *args, **kwargs) ...
▶ Local vars
/home/teo/MYWORKSPACE/dev/Lemix/Inventory/views.py in InsertModelPageForm
new_object2 = prfmodel.save() ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/forms/models.py in save
return self.save_existing_objects(commit) + self.save_new_objects(commit) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/forms/models.py in save_new_objects
self.new_objects.append(self.save_new(form, commit=commit)) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/forms/models.py in save_new
setattr(obj, self.fk.get_attname(), getattr(pk_value, 'pk', pk_value)) ...
▶ Local vars
并突出显示行new_object2 = prfmodel.save()
答案 0 :(得分:1)
来自django代码:
def save(self, commit=True):
"""
Save this form's self.instance object if commit=True. Otherwise, add
a save_m2m() method to the form which can be called after the instance
is saved manually at a later time. Return the model instance.
"""
检查返回模型实例。文本。
您正在尝试重写save方法,这会导致django ModelForm在您尝试提交表单时崩溃。