Ticket #34257: raw_fk_widget_missing_class.patch

File raw_fk_widget_missing_class.patch, 1.7 KB (added by Kevin Marsh, 22 months ago)
  • django/contrib/admin/widgets.py

    diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
    index c7d5db209e..a6d49bbce6 100644
    a b def get_context(self, name, value, attrs):  
    157157            css_class = "vForeignKeyRawIdAdminField"
    158158            if isinstance(self.rel.get_related_field(), UUIDField):
    159159                css_class += " vUUIDField"
    160             context["widget"]["attrs"].setdefault("class", css_class)
     160            if "class" in context["widget"]["attrs"]:
     161                context["widget"]["attrs"]["class"] += f" {css_class}"
     162            else:
     163                context["widget"]["attrs"]["class"] = css_class
    161164        else:
    162165            context["related_url"] = None
    163166        if context["widget"]["value"]:
  • tests/admin_widgets/tests.py

    diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py
    index 70b1233ef4..2c2318b28e 100644
    a b def test_render_fk_as_pk_model(self):  
    757757            'class="related-lookup" id="lookup_id_test" title="Lookup"></a>',
    758758        )
    759759
     760    def test_includes_class_override(self):
     761        rel = ReleaseEvent._meta.get_field("album").remote_field
     762        attrs = {"class": "myClass"}
     763        w = widgets.ForeignKeyRawIdWidget(rel, widget_admin_site, attrs=attrs)
     764        self.assertHTMLEqual(
     765            w.render("test", None),
     766            '<input type="text" name="test" class="myClass vForeignKeyRawIdAdminField">'
     767            '<a href="/admin_widgets/album/?_to_field=id" class="related-lookup" '
     768            'id="lookup_id_test" title="Lookup"></a>',
     769        )
     770
    760771
    761772@override_settings(ROOT_URLCONF="admin_widgets.urls")
    762773class ManyToManyRawIdWidgetTest(TestCase):
Back to Top