Code

Ticket #17190: 17190.diff

File 17190.diff, 2.1 KB (added by kenth, 2 years ago)

patch with tests

Line 
1diff --git a/django/forms/widgets.py b/django/forms/widgets.py
2index 5b50f37..8c1e20b 100644
3--- a/django/forms/widgets.py
4+++ b/django/forms/widgets.py
5@@ -552,7 +552,9 @@ class NullBooleanSelect(Select):
6     A Select Widget intended to be used with NullBooleanField.
7     """
8     def __init__(self, attrs=None):
9-        choices = ((u'1', ugettext('Unknown')), (u'2', ugettext('Yes')), (u'3', ugettext('No')))
10+        choices = ((u'1', ugettext_lazy('Unknown')),
11+                   (u'2', ugettext_lazy('Yes')),
12+                   (u'3', ugettext_lazy('No')))
13         super(NullBooleanSelect, self).__init__(attrs, choices)
14 
15     def render(self, name, value, attrs=None, choices=()):
16diff --git a/tests/regressiontests/forms/tests/widgets.py b/tests/regressiontests/forms/tests/widgets.py
17index 2424bea..a1d98e9 100644
18--- a/tests/regressiontests/forms/tests/widgets.py
19+++ b/tests/regressiontests/forms/tests/widgets.py
20@@ -984,6 +984,11 @@ beatle J R Ringo False""")
21         self.assertEqual(w.render('date', datetime.datetime(2007, 9, 17, 12, 51)), u'<input type="hidden" name="date_0" value="2007-09-17" /><input type="hidden" name="date_1" value="12:51:00" />')
22 
23 
24+class NullSelectNotLazyForm(Form):
25+    """Form to test for lazy evaluation #17190"""
26+    name = CharField(widget=NullBooleanSelect())
27+
28+
29 class FormsI18NWidgetsTestCase(TestCase):
30     def setUp(self):
31         super(FormsI18NWidgetsTestCase, self).setUp()
32@@ -1025,6 +1030,11 @@ class FormsI18NWidgetsTestCase(TestCase):
33         w.is_localized = True
34         self.assertEqual(w.render('date', datetime.datetime(2007, 9, 17, 12, 51)), u'<input type="hidden" name="date_0" value="17.09.2007" /><input type="hidden" name="date_1" value="12:51:00" />')
35 
36+    def test_nullbooleanselect(self):
37+        """test for lazy evaluation #17190"""
38+        f = NullSelectNotLazyForm()
39+        self.assertEqual(f.as_table(), u'<tr><th><label for="id_name">Name:</label></th><td><select name="name" id="id_name">\n<option value="1" selected="selected">Unbekannt</option>\n<option value="2">Ja</option>\n<option value="3">Nein</option>\n</select></td></tr>')
40+
41 
42 class SelectAndTextWidget(MultiWidget):
43     """