#31741: Admin site applies formfield_overrides for parent field class to
instance
of a child class
-----------------------------------------+------------------------
Reporter: Mark Gregson | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 2.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
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.
{{{#!python
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/31741>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/051.e7d3805ae9c1e7157591bb68b4a1f4e0%40djangoproject.com.