How in the admin filter a select for a models.CharField with choices

I would like to filter the item in the select box of the add/change view in the admin.

My model:

class Decision(models.Model):

    A1 = "A1"
    A2 = "A2"
    B1 = "B1"
    B2 = "B2"

    DECISION_CHOICES = [
        (B1, _('B1 - Closed')),
        (B2, _('B2 - Escalation of severity')),
        (A1, _('A1 - Closed with warning')),
        (A2, _('A2 - Review by certification committee')),
    ]

    ncf = models.OneToOneField(Ncf, on_delete=models.PROTECT)
    decision = models.CharField(max_length=2, choices=DECISION_CHOICES)
    sanction = RichTextField(blank=True, null=True, default=None)
    notes = models.TextField(blank=True, null=True)
    ...

in the admin (ModelAdmin) add/change page I would like to filter the choices of DECISION_CHOICES like something like this:

class DecisionAdmin(admin.ModelAdmin)

...
def ???(self, ....)
    if ncf.severity == 'A':
        DECISION_CHOICES = [
            (A1, _('A1 - Closed with warning')),
            (A2, _('A2 - Review by certification committee')),
        ]
    else:
        DECISION_CHOICES = [
            (B1, _('B1 - Closed')),
            (B2, _('B2 - Escalation of severity')),
        ]

...

Any Idea ? What can be overrided ?

Thx

I believe you would need to create a custom ModelForm for your ModelAdmin object. Within that form, you could override the __init__ method to assign the choices for that field.

Keep in mind that doing this is going to create a couple of difficult situations. For example, what should the decision choices be when you have not yet selected an NCF when you are creating a new instance? Or how are you going to enforce integrity if the severity field changes? If that field needs to be changed dynamically, then you’re looking at adding some JavaScript to your admin page now to manage it.

It’s not a good idea indeed…

I’ll make a validation in the clean() and or save() of the model instead.

Thanks for your time much appreciated.