2017-09-06 2 views
0

を必要と私は次のような形式があります除外フィールドはまだ

:私はフォームを送信すると、私はエラーを取得する

@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) 

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',) 

を私は次のモデルを持っています

post_typeフィールドエラー:このフィールドは必須です。

form.is_validメソッドのあとにこのフィールドを設定します。

このフィールドは必須フィールドタプルには含まれていないので、必須ではありませんか?

私はまた、追加しようとしている:私は同じエラーを取得しますが

post_type = models.IntegerField(choices=POST_CHOICES, blank=True) 

何か他には何かがありますか?

+0

post_typeをnullにしてからnullをtrueにする場合は、モデルで必要なフィールドとフィールドに異なるフィールドが必要です。そうでなければ、is_validの呼び出しの前にデータを設定できます。その後、あなたはこのフィールドを気にしないので、明らかに正しいでしょうか? – Quentin

答えて

1
post_type = forms.ChoiceField(widget=forms.RadioSelect(attrs={'name': 'radioInline'}), choices=POST_CHOICES, required=False) 

required=Falseは罰金になる追加


ごmodels.pyあなたはのModelFormでオーバーライドpost_typeフィールドを持っているので、動作しないと、あなたがしたい場合 required=False


にそれを設定しないでpost_type = models.IntegerField(choices=POST_CHOICES, blank=True)post_type = models.IntegerField(choices=POST_CHOICES, blank=True)仕事のみ:

class PostForm(forms.ModelForm): 

    class Meta: 
     model = Post 
     fields = ('title','desc','image','url', 'post_type') 
関連する問題