[Django] #31741: Admin site applies formfield_overrides for parent field class to instance of a child class

25 views
Skip to first unread message

Django

unread,
Jun 24, 2020, 9:17:31 PM6/24/20
to django-...@googlegroups.com
#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.

Django

unread,
Jun 25, 2020, 12:11:32 AM6/25/20
to django-...@googlegroups.com
#31741: Admin site applies formfield_overrides for parent field class to instance
of a child class
-------------------------------+--------------------------------------

Reporter: Mark Gregson | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 2.2
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => wontfix


Comment:

It is expected behavior, see #10059.
[https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_overrides
Docs] improvements are always welcome, please feel-free to send PR with
clarification.

--
Ticket URL: <https://code.djangoproject.com/ticket/31741#comment:1>

Django

unread,
Jun 25, 2020, 12:23:59 AM6/25/20
to django-...@googlegroups.com
#31741: Admin site applies formfield_overrides for parent field class to instance
of a child class
-------------------------------+--------------------------------------

Reporter: Mark Gregson | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 2.2
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Mark Gregson):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/31741#comment:2>

Reply all
Reply to author
Forward
0 new messages