#19303: ModelAdmin.formfield_overrides is ignored for fields with choices
-------------------------------+------------------------------------
Reporter: bendavis78 | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by aaugustin):
* needs_better_patch: 0 => 1
* stage: Unreviewed => Accepted
Comment:
I assume you meant:
{{{
formfield_overrides = {
MyCustomField: {'widget': MyCustomWidget}
}
}}}
The patch isn't acceptable because it breaks a test:
{{{
(django-dev)myk@mYk tests % PYTHONPATH=.. ./runtests.py
--settings=test_sqlite admin_widgets
~/Documents/dev/django/tests
Creating test database for alias 'default'...
Creating test database for alias 'other'...
..........F....................sss.......sss...
======================================================================
FAIL: testFieldWithChoices
(regressiontests.admin_widgets.tests.AdminFormfieldForDBFieldTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File
"/Users/myk/Documents/dev/django/tests/regressiontests/admin_widgets/tests.py",
line 116, in testFieldWithChoices
self.assertFormfield(models.Member, 'gender', forms.Select)
File
"/Users/myk/Documents/dev/django/tests/regressiontests/admin_widgets/tests.py",
line 58, in assertFormfield
(model.__class__.__name__, fieldname, widgetclass, type(widget))
AssertionError: Wrong widget for ModelBase.gender: expected <class
'django.forms.widgets.Select'>, got <class
'django.contrib.admin.widgets.AdminTextInputWidget'>
----------------------------------------------------------------------
Ran 41 tests in 0.430s
FAILED (failures=1, skipped=6)
Destroying test database for alias 'default'...
Destroying test database for alias 'other'...
}}}
----
For consistency between `formfield_for_choice_field`,
`formfield_for_foreignkey` and `formfield_for_manytomany`, the fix should
move the handling of `formfield_overrides` either up, at the beginning of
`formfield_for_dbfield`, or down, in a common method that would wrap
`db_field.formfield` and be called by each `formfield_to_*`. Actually it
would have to do both, see below.
The expected order of precendence is:
- `formfield_overrides`
- values set by `formfield_for_*`
- FORMFIELD_FOR_DBFIELD_DEFAULTS
This can't be achieved without some refactoring,
because`formfield_overrides` is currently merged with
`FORMFIELD_FOR_DBFIELD_DEFAULTS`.
This code dates back to f212b24b6469b66424354bf970f3051df180b88d.
Here's how I would fix the problem:
- merge `formfield_overrides` in `kwargs` at the very beginning of
`formfield_for_dbfield`
- add a wrapper around that merges `FORMFIELD_FOR_DBFIELD_DEFAULTS` in
`kwargs` and then calls `db_field.formfield`
- write tests.
--
Ticket URL: <https://code.djangoproject.com/ticket/19303#comment:2>
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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.