[Django]-Dynamically alter Field choices in Django ModelForm

13👍

Then the form is initialized the choices property is copied from the field to the field’s widget. So you have to remove the choice from the widget too:

if self.instance and self.instance.status_field is not None:
    new_choices = list(self.fields['status_field'].choices)
    new_choices.remove((None, 'Pending'))
    self.fields['status_field'].choices = new_choices
    self.fields['status_field'].widget.choices = new_choices

Leave a comment