0
我有以下形式:排除领域仍然需要
class PostForm(forms.ModelForm):
post_type = forms.ChoiceField(widget=forms.RadioSelect(attrs={'name': 'radioInline'}), choices=POST_CHOICES)
class Meta:
model = Post
fields = ('title','desc','image','url',)
我有以下型号:
@python_2_unicode_compatible
class Post(models.Model):
entity = models.ForeignKey('companies.Entity')
title = models.CharField('Post Title', max_length=128, unique=True)
desc = models.TextField('Description', blank=True, null=True)
post_type = models.IntegerField(choices=POST_CHOICES)
image = models.ImageField('Post Image', upload_to='post', blank=True, null=True)
url = models.URLField(max_length=255, blank=True, null=True)
slug = models.SlugField(blank=True, null=True, unique=True)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
当我提交表单,我得到的错误:
post_type字段错误:该字段是必需的。
我想在form.is_valid方法之后填充这个字段。
由于此字段不在所需的字段元组中,是否不需要它?
我也尝试添加:
post_type = models.IntegerField(choices=POST_CHOICES, blank=True)
虽然我得到同样的错误。
还有别的事情吗?
如果您希望post_type为null,然后添加null = True,那么在模型中所需的表单和字段之间存在不同的字段,否则您可以在调用is_valid之前填充它,这不会影响附加内容因为你不关心这个领域显然是对的? – Quentin