Django: Make certain fields in a ModelForm required=False

后端 未结 5 1939
无人共我
无人共我 2021-02-02 06:08

How do I make certain fields in a ModelForm required=False?

If I have:

class ThatForm(ModelForm):
  class Meta:
    widgets = {\"text\": Textarea(require         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-02 06:47

    following from comments. Probably yes:

    class ThatForm(ModelForm):
        def __init__(self, *args, **kwargs):
            # first call parent's constructor
            super(ThatForm, self).__init__(*args, **kwargs)
            # there's a `fields` property now
            self.fields['desired_field_name'].required = False
    

提交回复
热议问题