diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index 0d1f2a9ff7..29958b27a4 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -53,8 +53,11 @@ class AdminDateWidget(forms.DateInput): js = ["calendar.js", "admin/DateTimeShortcuts.js"] return forms.Media(js=[static("admin/js/%s" % path) for path in js]) - def __init__(self, attrs={}, format=None): - super(AdminDateWidget, self).__init__(attrs={'class': 'vDateField', 'size': '10'}, format=format) + def __init__(self, attrs=None, format=None): + final_attrs = {'class': 'vDateField', 'size': '10'} + if attrs is not None: + final_attrs.update(attrs) + super(AdminDateWidget, self).__init__(attrs=final_attrs, format=format) class AdminTimeWidget(forms.TimeInput): @@ -63,8 +66,11 @@ class AdminTimeWidget(forms.TimeInput): js = ["calendar.js", "admin/DateTimeShortcuts.js"] return forms.Media(js=[static("admin/js/%s" % path) for path in js]) - def __init__(self, attrs={}, format=None): - super(AdminTimeWidget, self).__init__(attrs={'class': 'vTimeField', 'size': '8'}, format=format) + def __init__(self, attrs=None, format=None): + final_attrs = {'class': 'vTimeField', 'size': '8'} + if attrs is not None: + final_attrs.update(attrs) + super(AdminTimeWidget, self).__init__(attrs=final_attrs, format=format) class AdminSplitDateTime(forms.SplitDateTimeWidget): """ diff --git a/tests/regressiontests/admin_widgets/tests.py b/tests/regressiontests/admin_widgets/tests.py index 08a1a59766..37fa7bc8ef 100644 --- a/tests/regressiontests/admin_widgets/tests.py +++ b/tests/regressiontests/admin_widgets/tests.py @@ -205,6 +205,41 @@ class FilteredSelectMultipleWidgetTest(DjangoTestCase): '\n' % admin_media_prefix() ) +class AdminDateWidgetTest(DjangoTestCase): + def test_attrs(self): + """ + Ensure that user-supplied attrs are used. + Refs #12073. + """ + w = widgets.AdminDateWidget() + self.assertEqual( + conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))), + '', + ) + # pass attrs to widget + w = widgets.AdminDateWidget(attrs={'size': 20, 'class': 'myDateField'}) + self.assertEqual( + conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))), + '', + ) + +class AdminTimeWidgetTest(DjangoTestCase): + def test_attrs(self): + """ + Ensure that user-supplied attrs are used. + Refs #12073. + """ + w = widgets.AdminTimeWidget() + self.assertEqual( + conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))), + '', + ) + # pass attrs to widget + w = widgets.AdminTimeWidget(attrs={'size': 20, 'class': 'myTimeField'}) + self.assertEqual( + conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))), + '', + ) class AdminSplitDateTimeWidgetTest(DjangoTestCase): def test_render(self):