Django modelform NOT required field

Andres picture Andres · Apr 25, 2013 · Viewed 68.2k times · Source

I have a form like this:

class My_Form(ModelForm):
    class Meta:
        model = My_Class
        fields = ('first_name', 'last_name' , 'address')

How can I put the address field as optional?

Answer

madzohan picture madzohan · May 22, 2015
class My_Form(forms.ModelForm):
    class Meta:
        model = My_Class
        fields = ('first_name', 'last_name' , 'address')

    def __init__(self, *args, **kwargs):
        super(My_Form, self).__init__(*args, **kwargs)
        self.fields['address'].required = False