- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个 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) 时,它确实将“cleaned_data”显示为属性之一,而在 Django 1.11 中却没有。
Django 1.11 中的“cleaned_data”消失在哪里?
编辑:添加回溯:
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'
最佳答案
表单集在 1.11 ( in #26844 ) 中进行了修复,以在验证最小数量的表单时忽略空表单。作为副作用,表单集现在在验证表单之前对每个表单调用 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()
中所做的事情,但将此代码放在其他地方很可能是个好主意。
关于python - clean_data 在 Django 1.11 中消失在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43320603/
我有一个 model_formset,使用 can_delete=True 创建,并将相应的 form.DELETE 复选框添加到模板中。当我收到 POST 请求时,我尝试保存表单: if forms
我正在 django 中做一些密码确认表单。但我很困惑,因为 self.cleaned_data.get('confirm_password') 总是不返回任何内容,因此我的密码永远不会相互匹配。 这
我创建了一个 inlineformset_factory 如下: formset = inlineformset_factory(Author, Book, form=BookForm,
我正在尝试为我的一门类(class)制作搜索表单。表格的型号是: from django import forms from django.forms import CharField, ModelM
我正在编写 django 教程,并尝试使用 clean_data 属性验证表单。 forms.py 文件的代码是 from django import forms from .models imp
我是一名优秀的程序员,十分优秀!