gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:07:20 28 4
gpt4 key购买 nike

我正在为 Django 表单编写测试。我正在用初始数据填充它。但是,当我保存表单时,出现错误,指出它没有 cleaned_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。如果我将它放在初始数据中,则不起作用。

最佳答案

您还没有向表单传递任何数据,只有初始值。因此该表格未绑定(bind)且无效。

关于python - Django 模型表单 - 没有任何错误无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39062172/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com