From a2f7433adc610cb0a85b3e23a1fec2267591f866 Mon Sep 17 00:00:00 2001 From: Artem Tabolin Date: Thu, 15 Mar 2018 15:55:34 +0100 Subject: [PATCH] Fixed #29221 -- Corrected admin's autocomplete widget to add a space after custom classes. --- django/contrib/admin/widgets.py | 2 +- docs/releases/2.0.4.txt | 3 +++ tests/admin_widgets/test_autocomplete_widget.py | 7 ++++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index 271bbefb99..5af187a5c3 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -413,7 +413,7 @@ class AutocompleteMixin: 'data-theme': 'admin-autocomplete', 'data-allow-clear': json.dumps(not self.is_required), 'data-placeholder': '', # Allows clearing of the input. - 'class': attrs['class'] + 'admin-autocomplete', + 'class': attrs['class'] + (' ' if attrs['class'] else '') + 'admin-autocomplete', }) return attrs diff --git a/docs/releases/2.0.4.txt b/docs/releases/2.0.4.txt index ce7a495a6d..be538b46fd 100644 --- a/docs/releases/2.0.4.txt +++ b/docs/releases/2.0.4.txt @@ -14,3 +14,6 @@ Bugfixes * Fixed admin autocomplete widget's translations for `zh-hans` and `zh-hant` languages (:ticket:`29213`). + +* Corrected admin's autocomplete widget to add a space after custom classes + (:ticket:`29221`). diff --git a/tests/admin_widgets/test_autocomplete_widget.py b/tests/admin_widgets/test_autocomplete_widget.py index 32c0964d87..cd9d58e347 100644 --- a/tests/admin_widgets/test_autocomplete_widget.py +++ b/tests/admin_widgets/test_autocomplete_widget.py @@ -50,7 +50,7 @@ class AutocompleteMixinTests(TestCase): form = AlbumForm() attrs = form['band'].field.widget.get_context(name='my_field', value=None, attrs={})['widget']['attrs'] self.assertEqual(attrs, { - 'class': 'my-classadmin-autocomplete', + 'class': 'my-class admin-autocomplete', 'data-ajax--cache': 'true', 'data-ajax--type': 'GET', 'data-ajax--url': '/admin_widgets/band/autocomplete/', @@ -59,6 +59,11 @@ class AutocompleteMixinTests(TestCase): 'data-placeholder': '' }) + def test_build_attrs_no_custom_class(self): + form = AlbumForm() + attrs = form['featuring'].field.widget.get_context(name='name', value=None, attrs={})['widget']['attrs'] + self.assertEqual(attrs['class'], 'admin-autocomplete') + def test_build_attrs_not_required_field(self): form = NotRequiredBandForm() attrs = form['band'].field.widget.build_attrs({})