Django模型表单 - 没有任何错误无效

时间:2016-08-21 08:09:32

标签: python django

我正在为Django表单编写测试。我用初始数据填充它。但是当我保存表单时,我得到的错误是它没有clean_data属性。

可能会发生这种情况,因为表单没有验证,但也没有显示任何错误。

这是代码。

def test_keyw(self):
    class BlogPostKeywordCheck(forms.ModelForm):
        class Meta:
            model = BlogPost
            exclude = ()

    data = {'keywords': 'awwww,aaa,lol'}


    initial_data = {
        "title":"Test Keywords",
        "content":"<p>Testing Keywords</p>",
        "status":CONTENT_STATUS_PUBLISHED,
        "keywords":"call,me,abc",
        "user":self._user,
        "allow_comments":"on",
        "gen_description":"on",
        "in_sitemap":"on",
        "_save":"Save"
    }

    print (self._user)
    submitted_form = BlogPostKeywordCheck(initial=initial_data)

    print (submitted_form.fields)

    submitted_form.instance.user = self._user
    print("Instance Title",submitted_form.instance.title)


    print("Valid: ",submitted_form.is_valid())

    print ("Errors: ",submitted_form.errors)
    submitted_form.save()

    print (Keyword.objects.all())
    self.assertTrue(submitted_form.is_valid())
    print (submitted_form.errors)

目前O / P是

Creating test database for alias 'default'...
test
OrderedDict([('title', <django.forms.fields.CharField object at 0x05185CF0>), ('slug', <django.forms.fields.CharField object at 0x05185BD0>), ('_meta_title', <django.forms.fields.CharField object at 0x05185ED0>), ('description', <django.forms.fields.CharField object at 0x05185E10>), ('gen_description', <django.forms.fields.BooleanField object at 0x05185C10>), ('keywords', <django.forms.fields.CharField object at 0x05185CD0>), ('status', <django.forms.fields.TypedChoiceField object at 0x05185C50>), ('publish_date', <django.forms.fields.DateTimeField object at 0x05185B10>), ('expiry_date', <django.forms.fields.DateTimeField object at 0x05185D90>), ('short_url', <django.forms.fields.URLField object at 0x05185AD0>), ('in_sitemap', <django.forms.fields.BooleanField object at 0x05185FD0>), ('content', <django.forms.fields.CharField object at 0x05182E50>), ('user', <django.forms.models.ModelChoiceField object at 0x05182350>), ('categories', <django.forms.models.ModelMultipleChoiceField object at 0x051820D0>), ('allow_comments', <django.forms.fields.BooleanField object at 0x051821F0>), ('featured_image', <filebrowser_safe.fields.FileBrowseFormField object at 0x05182310>), ('related_posts', <django.forms.models.ModelMultipleChoiceField object at 0x051822B0>)])
(u'Instance Title', u'')
(u'Valid: ', False)
(u'Errors: ', {})
Destroying test database for alias 'default'...

Process finished with exit code 1

Error
Traceback (most recent call last):
  File "F:\Projects\GIT\mezzanine\build\build1\mezzanine\generic\tests.py", line 226, in test_keyw
    submitted_form.save()
  File "C:\Python27\lib\site-packages\django\forms\models.py", line 449, in save
    self._save_m2m()
  File "C:\Python27\lib\site-packages\django\forms\models.py", line 416, in _save_m2m
    cleaned_data = self.cleaned_data
AttributeError: 'BlogPostKeywordCheck' object has no attribute 'cleaned_data'

如果我没有在实例中显式添加user_id,它告诉我User_id不能为null。如果我把它放在初始数据中不起作用。

1 个答案:

答案 0 :(得分:2)

您尚未将任何数据传递给表单,只传递初始值。因此,表格不受约束,无效。