[Fixed]-Dynamically excluding field from Django ModelForm

13👍

You can solve it this way:

class RandomForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(RandomForm, self).__init__(*args, **kwargs)
        if not self.instance:
            self.fields.pop('active')

    class Meta:
        model = models.Service
        fields = (...some fields...)

-5👍

Django ModelForm provides exclude attribute. Have you tried that?

class RandomForm(ModelForm):

    class Meta:
        model = models.Service
        exclude = ['is_active']

Leave a comment