[Django] #34257: ForeignKeyRawIdWidget doesn't include vForeignKeyRawIdAdminField class when custom class passed in attrs

15 views
Skip to first unread message

Django

unread,
Jan 13, 2023, 5:30:44 PM1/13/23
to django-...@googlegroups.com
#34257: ForeignKeyRawIdWidget doesn't include vForeignKeyRawIdAdminField class when
custom class passed in attrs
-------------------------------------+-------------------------------------
Reporter: Kevin | Owner: nobody
Marsh |
Type: Bug | Status: new
Component: | Version: dev
contrib.admin | Keywords:
Severity: Normal | widget,admin,raw_id_fields
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I had a situation where I wanted to add an extra class to the
`ForeignKeyRawIdWidget` widget. I noticed that doing something like:

{{{
attrs = {"class": "myAdditionalClass"}
widget = ForeignKeyRawIdWidget(rel, admin_site, attrs=attrs)
}}}

resulted in the widget not having the required
`vForeignKeyRawIdAdminField` class since under the hood
`ForeignKeyRawIdWidget.get_context` does something ''like:''

{{{
context["widget"]["attrs"].setdefault("class",
"vForeignKeyRawIdAdminField")
}}}

meaning the rendered `<input />` looks like:
{{{
<input class="myAdditionalClass" ...
}}}

instead of:
{{{
<input class="myAdditionalClass vForeignKeyRawIdAdminField" ...
}}}

----

Note that this same issue applies to the `ManyToManyRawIdWidget` and
corresponding `vManyToManyRawIdAdminField` class.

----

I'm not sure if `ForeignKeyRawIdWidget`/`ManyToManyRawIdWidget` is meant
to be public, but it'd be nice if we could resolve this in the source.
Currently I can work around this by simply doing:
{{{
attrs = {"class": "myAdditionalClass vForeignKeyRawIdAdminField"}
widget = ForeignKeyRawIdWidget(rel, admin_site, attrs=attrs)
}}}

I can create a PR based on a similar solution in the patch (and also fix
`ManyToManyRawIdWidget`) if this gets accepted.

--
Ticket URL: <https://code.djangoproject.com/ticket/34257>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 13, 2023, 5:31:11 PM1/13/23
to django-...@googlegroups.com
#34257: ForeignKeyRawIdWidget doesn't include vForeignKeyRawIdAdminField class when
custom class passed in attrs
-------------------------------------+-------------------------------------
Reporter: Kevin Marsh | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage:
widget,admin,raw_id_fields | Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Kevin Marsh):

* Attachment "raw_fk_widget_missing_class.patch" added.

Django

unread,
Jan 13, 2023, 5:47:39 PM1/13/23
to django-...@googlegroups.com

Django

unread,
Jan 13, 2023, 5:47:40 PM1/13/23
to django-...@googlegroups.com
#34257: ForeignKeyRawIdWidget doesn't include vForeignKeyRawIdAdminField class when
custom class passed in attrs
-------------------------------------+-------------------------------------
Reporter: Kevin Marsh | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage:
widget,admin,raw_id_fields | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Kevin Marsh):

* Attachment "raw_fk_widget_missing_class.patch" removed.

Django

unread,
Jan 14, 2023, 7:41:38 AM1/14/23
to django-...@googlegroups.com
#34257: ForeignKeyRawIdWidget doesn't include vForeignKeyRawIdAdminField class when
custom class passed in attrs
-------------------------------------+-------------------------------------
Reporter: Kevin Marsh | Owner: nobody
Type: New feature | Status: closed
Component: contrib.admin | Version: dev
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
widget,admin,raw_id_fields | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* type: Bug => New feature
* has_patch: 1 => 0
* resolution: => wontfix


Comment:

I don't see any issue here, TBH. As far as I'm aware, attributes passed in
`attrs` override default values for all widgets (not only widgets defined
in `django.contrib.admin.widgets`), any change to this logic would be
backward incompatible.

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

Django

unread,
Jan 16, 2023, 1:44:59 PM1/16/23
to django-...@googlegroups.com
#34257: ForeignKeyRawIdWidget doesn't include vForeignKeyRawIdAdminField class when
custom class passed in attrs
-------------------------------------+-------------------------------------
Reporter: Kevin Marsh | Owner: nobody
Type: New feature | Status: closed
Component: contrib.admin | Version: dev
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
widget,admin,raw_id_fields | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Kevin Marsh):

Replying to [comment:1 Mariusz Felisiak]:

Thanks Mariusz, hopefully at least this ticket will serve as instructions
for others about how to ensure the JS in the widget still works when
adding additional classes (although is at risk of breaking if
`vForeignKeyRawIdAdminField` is ever renamed in a new version of Django).

> As far as I'm aware, attributes passed in attrs override default values
for all widgets

Not sure that's entirely true, eg. I think something like
`FilteredSelectMultiple` just ignores any "class" attr:

{{{
class FilteredSelectMultipleWidgetTest(SimpleTestCase):
#... rest of existing test class
def test_render_ignores_additional_attrs(self):
# This widget ignores any class override in `attrs`
w = widgets.FilteredSelectMultiple("test", False, attrs={"class":
"myAdditionalClass"})
self.assertHTMLEqual(
w.render("test", "test"),
'<select multiple name="test" class="selectfilter" '
'data-field-name="test" data-is-stacked="0">\n</select>',
)
}}}

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

Reply all
Reply to author
Forward
0 new messages