我创建了一个inlineformset_factory,如下所示:
formset = inlineformset_factory(Author, Book, form=BookForm,
formset=BaseBookFormSet,
can_order=False, can_delete=True,
extra=1, fields=('id', name)
)
BookForm如下:
class BookForm(forms.ModelForm):
name = forms.Charfield(required=True)
def __init__(self, *args, **kwargs):
super(BookForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
Div(
Field("id", type="hidden"),
Field("name"),
Field("DELETE")
)
)
class Meta:
model = Book
fields = ('id', 'name')
def clean_name(self):
book_name = self.cleaned_data['name']
try:
book = Book.objects.get(name=book_name)
return book
except:
return book_name
def clean(self):
cleaned_data = super(BookForm, self).clean()
... other operations on cleaned_data ...
def has_changed(self):
changed = super(BookForm, self).has_changed()
cleaned_data = self.clean()
... other code here ...
这在提交表单时出错:
Exception Type: AttributeError
Exception Value: 'BookForm' object has no attribute 'cleaned_data'
在views.py中调用formset.is_valid()时。 Traceback首先显示has_changed中调用self.clean的行,然后调用clean()中调用super clean的行。
这曾经在django 1.10中正常工作。
当我在Django 1.10中尝试打印dir(self)时,它会显示' cleaning_data'作为Django 1.11中的属性之一,它没有。
清洁_数据'在哪里?在Django 1.11中消失了吗?
编辑:添加追溯:
Traceback (most recent call last):
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/vagrant/test_os/inventory/views.py", line 297, in post
if formset.is_valid():
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/forms/formsets.py", line 321, in is_valid
self.errors
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/forms/formsets.py", line 295, in errors
self.full_clean()
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/forms/formsets.py", line 345, in full_clean
if not form.has_changed():
File "/vagrant/test_os/inventory/forms.py", line 220, in has_changed
cleaned_data = self.clean()
File "/vagrant/test_os/inventory/forms.py", line 177, in clean
cleaned_data = super(BookForm, self).clean()
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/forms/models.py", line 344, in clean
return self.cleaned_data
AttributeError: 'BookForm' object has no attribute 'cleaned_data'
答案 0 :(得分:3)
在验证最小数量的表单时,在1.11(in #26844)中修复了表单集以忽略空表单。作为副作用,formsets现在在验证表单之前在每个表单上调用form.has_changed()
。 Django希望form.has_changed()
在表单验证之前可以安全地调用,并且默认实现确实可以安全地调用。
您已覆盖form.has_changed()
来调用self.clean()
,现在会在验证表单之前发生。由于form.clean()
要求验证表单,因此失败。
由于form.full_clean()
实际上会调用self.has_changed()
,因此您无法在form.has_changed()
内简单地验证表单。您不会在has_changed()
中显示您的操作,但将此代码放在其他位置最有可能是个好主意。