ModelMultipleChoiceField - cleaning_data没有值

时间:2017-05-30 19:23:52

标签: python django-models django-forms choicefield

我想制作包含帖子和标签的简单博客,但是我在调​​用.is_valid()后从ModelMultipleChoiceField添加标签时遇到问题,cleaning_data应该选择标签但是它是None。

// Doesn't match
brett@gmail.com
smith@gmail.com

// Matches
brettt@gmail.com
bret.t@gmail.com
_brett@gmail.com
bretty@gmail.com
brett_@gmail.com
brett99@gmail.com
test@gmail.com
john_doe.77@gmail.com

返回:

print(form.cleaned_data) 

我尝试将其更改为简单的ChoiceField,其中包含来自表的初始选择,但结果相同。

我的模特:

{'description': u'xyz', 'title': u'xyz', 'haslo': u'', 'tags': None}

形式:

class Tag(models.Model):
    nazwa = models.CharField(max_length=24)

    def __str__(self):
        return self.nazwa

    def __unicode__(self):
        return self.nazwa

class Post(models.Model):
    tytul = models.CharField(max_length=200)
    skrocona_tresc = models.CharField(max_length=350, blank=True)
    tresc = models.TextField(blank=True)
    haslo = models.CharField(max_length=24, blank=True)
    data = models.DateTimeField(auto_now=False, auto_now_add=True)
    id_user = models.ForeignKey(User, on_delete=models.CASCADE)
    tags = models.ManyToManyField(Tag)

并查看:

class PostAddForm(forms.ModelForm):
    title = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}),required=False)
    password = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'form-control'}),required=False)
    description = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}),required=False)
    tags = forms.ModelMultipleChoiceField(queryset=Tag.objects.all(), required=False)
    class Meta:
        model = Post
        fields = [
            'title',
            'password',
            'description',
            'tags'
        ]

我刚开始使用Django; /

0 个答案:

没有答案