[Answered ]-Simplest way to override Django admin inline to request formfield_for_dbfield for each instance

2👍

The way I eventually solved this is using the form = attribute of the Admin Inline. This skips the form generation code of the ModelAdmin:

class SpecificationValueForm(ModelForm):
    class Meta:
        model = SpecificationValue

    def __init__(self, instance=None, **kwargs):
        super().__init__(instance=instance, **kwargs)
        if instance:
            self.fields['value'] = instance.parameter.get_value_formfield()
        else:
            self.fields['value'].disabled = True

class SpecificationValueAdminInline(admin.TabularInline):
    form = SpecificationValueForm

Using standard forms like this, widgets with choices (e.g. RadioSelect and CheckboxSelectMultiple) have list bullets next to them in the admin interface because the <ul> doesn’t have the radiolist class. You can almost fix the RadioSelect by using AdminRadioSelect(attrs={'class': 'radiolist'}) but there isn’t an admin version of the CheckboxSelectMultiple so I preferred consistency. Also there is an aligned class missing from the <fieldset> wrapper element.

Looks like I’ll have to live with that!

Leave a comment