2016-08-21 37 views
2

我正在爲Django表單編寫測試。我正在使用初始數據填充它。但是當我保存表單時,我得到的錯誤是它沒有clean_data屬性。Django模型表單 - 無效且沒有任何錯誤

可能發生這種情況,因爲表單沒有驗證,但也沒有顯示任何錯誤。

這裏是代碼。

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不能爲空。如果我將其放入初始數據中,則不起作用。

回答

2

您尚未將任何數據傳遞給表單,只有初始值。因此表單不受限制,不能有效。

+0

我已經提供了初始數據,表單沒有綁定任何特定的實例,對吧?它一旦保存就應該被綁定。 –

+0

不,這不是它的工作原理;請參閱[文檔](https://docs.djangoproject.com/en/1.10/ref/forms/api/#ref-forms-api-bound-unbound)。將數據傳遞給它時,表單會被綁定,而您沒有。 –

相關問題