考虑以下stacktrace:
In [3]: f.clean()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\core\management\commands\shell.pyc in <module>()
----> 1 f.clean()
C:\Users\Marcin\Documents\oneclickcos\oneclickcos\mainapp\incorporate_helpers.pyc in clean(self)
569 This checks the relations between fields, ensures consistent state, and exports bits about the state of the form that can be used in subsequent
validations
570 """
--> 571 cleaned_data = super(IncorporateForm, self).clean()
572 #logger.debug('IncorporationForm.cleaned_data: ' + str(cleaned_data))
573 try:
C:\Users\Marcin\Documents\oneclickcos\oneclickcos\mainapp\incorporate_helpers.pyc in clean(self)
402
403 def clean(self):
--> 404 cleaned_data = super(CreateForm, self).clean()
405 # trying to use an empty pk can result in a ValueError. Easier to expunge.
406 if cleaned_data.get('pk', None) == '': del cleaned_data['pk']
C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\forms\forms.pyc in clean(self)
315 association with the field named '__all__'.
316 """
--> 317 return self.cleaned_data
318
319 def has_changed(self):
AttributeError: 'IncorporateForm' object has no attribute 'cleaned_data'
(所有相关代码均显示在上面的描述中)。
我的代码符合https://docs.djangoproject.com/en/1.4/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other的示例;但是,似乎BaseForm返回期望cleaned_data
存在。
在文档中我没有注意到在调用cleaned_data
之前创建clean
是用户代码的责任。是什么赋予了?这是Django中的错误吗?
django 1.4和1.3.1出现错误。
答案 0 :(得分:3)
您根本不应直接致电clean
。你应该致电form.is_valid()
。
答案 1 :(得分:2)
据我所知,文档从未建议您直接致电form.clean()
。大部分时间(所有时间?),您想要致电form.is_valid()
。
在幕后,调用is_valid()
会导致调用full_clean()
方法,该方法会设置cleaned_data
。