Remove fields from ModelForm

前端 未结 5 990
忘掉有多难
忘掉有多难 2020-12-09 06:16

i have a simple ModelForm:

class MyForm(ModelForm):

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        d         


        
5条回答
  •  情书的邮戳
    2020-12-09 06:26

    As described in Creating forms from models - Selecting the fields to use, there are three ways:

    1. In the model, set editable=False. All forms created from the model will exclude the field.
    2. Define the fields attribute in the Meta inner class to only include the fields you want.
    3. Define the exclude attribute in the Meta inner class to list the fields you don't want.

    So if your model has fields field1, field2, and field3 and you don't want field3, technique #2 would look like this:

    class MyModelForm(ModelForm):
        class Meta:
            model = MyModel
            fields = ('field1', 'field2')
    

    And technique #3 would look like this:

    class MyModelForm(ModelForm):
        class Meta:
            model = MyModel
            exclude = ('field3',)
    

提交回复
热议问题