Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#31741 closed Bug (wontfix)

Admin site applies formfield_overrides for parent field class to instance of a child class

Reported by: Mark Gregson Owned by: nobody
Component: contrib.admin Version: 2.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If I have a custom model field that inherits from a builtin field and then use formfield_overrides to customise the form for the builtin field, those customisations are also applied to the custom field.

In this example, the override will also apply to a CustomField field on the model.

class CustomField(models.TextField):
    ...

class SomeModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.TextField: {"widget": forms.Textarea},
    }

In my situation I am using a custom bleached field that sets a different default widget in its formfield method and it is counterintuitive to me that overriding TextField also overrides the custom implementation. I expect that it would only override fields matching the class precisely, ie, override fields declared as TextField instances but not those declared as CustomField.

The code that implements this behaviour is BaseModelAdmin.formfield_for_dbfield() which traverses the MRO looking for any matching overrides. https://github.com/django/django/blob/master/django/contrib/admin/options.py#L179-L184

I discovered it working with 2.2. The code looks unchanged from 2.2 to master, although I haven't tested in master.

If there is no appetite to change this behaviour, could break some folk's implementations, a note in the docs would be nice.

Change History (2)

comment:1 by Mariusz Felisiak, 4 years ago

Resolution: wontfix
Status: newclosed

It is expected behavior, see #10059. Docs improvements are always welcome, please feel-free to send PR with clarification.

comment:2 by Mark Gregson, 4 years ago

It's unfortunate that these different use-cases for creating custom model fields are not handled equally well by the admin options, but thanks for the information, I'll have a look at writing a note for the docs.

Note: See TracTickets for help on using tickets.
Back to Top