Code

Ticket #7190: django-real-bools.3.diff

File django-real-bools.3.diff, 5.5 KB (added by Alex, 4 years ago)
Line 
1diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
2index 05f16db..02c69c2 100644
3--- a/django/db/models/fields/__init__.py
4+++ b/django/db/models/fields/__init__.py
5@@ -524,9 +524,14 @@ class BooleanField(Field):
6         return "BooleanField"
7 
8     def to_python(self, value):
9-        if value in (True, False): return value
10-        if value in ('t', 'True', '1'): return True
11-        if value in ('f', 'False', '0'): return False
12+        if value in (True, False):
13+            # if value is 1 or 0 than it's equal to True or False, but we want
14+            # to return a true bool for semantic reasons.
15+            return bool(value)
16+        if value in ('t', 'True', '1'):
17+            return True
18+        if value in ('f', 'False', '0'):
19+            return False
20         raise exceptions.ValidationError(self.error_messages['invalid'])
21 
22     def get_prep_lookup(self, lookup_type, value):
23@@ -934,10 +939,16 @@ class NullBooleanField(Field):
24         return "NullBooleanField"
25 
26     def to_python(self, value):
27-        if value in (None, True, False): return value
28-        if value in ('None',): return None
29-        if value in ('t', 'True', '1'): return True
30-        if value in ('f', 'False', '0'): return False
31+        if value is None:
32+            return None
33+        if value in (True, False):
34+            return bool(value)
35+        if value in ('None',):
36+            return None
37+        if value in ('t', 'True', '1'):
38+            return True
39+        if value in ('f', 'False', '0'):
40+            return False
41         raise exceptions.ValidationError(self.error_messages['invalid'])
42 
43     def get_prep_lookup(self, lookup_type, value):
44diff --git a/docs/ref/databases.txt b/docs/ref/databases.txt
45index afead83..3aef61c 100644
46--- a/docs/ref/databases.txt
47+++ b/docs/ref/databases.txt
48@@ -323,16 +323,6 @@ storage engine, you have a couple of options.
49 Notes on specific fields
50 ------------------------
51 
52-Boolean fields
53-~~~~~~~~~~~~~~
54-
55-Since MySQL doesn't have a direct ``BOOLEAN`` column type, Django uses a
56-``TINYINT`` column with values of ``1`` and ``0`` to store values for the
57-:class:`~django.db.models.BooleanField` model field. Refer to the documentation
58-of that field for more details, but usually this won't be something that will
59-matter unless you're printing out the field values and are expecting to see
60-``True`` and ``False.``.
61-
62 Character fields
63 ~~~~~~~~~~~~~~~~
64 
65diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt
66index cd66c46..2311bf4 100644
67--- a/docs/ref/models/fields.txt
68+++ b/docs/ref/models/fields.txt
69@@ -342,19 +342,6 @@ A true/false field.
70 
71 The admin represents this as a checkbox.
72 
73-.. admonition:: MySQL users..
74-
75-    A boolean field in MySQL is stored as a ``TINYINT`` column with a value of
76-    either 0 or 1 (most databases have a proper ``BOOLEAN`` type instead). So,
77-    for MySQL, only, when a ``BooleanField`` is retrieved from the database
78-    and stored on a model attribute, it will have the values 1 or 0, rather
79-    than ``True`` or ``False``. Normally, this shouldn't be a problem, since
80-    Python guarantees that ``1 == True`` and ``0 == False`` are both true.
81-    Just be careful if you're writing something like ``obj is True`` when
82-    ``obj`` is a value from a boolean attribute on a model. If that model was
83-    constructed using the ``mysql`` backend, the "``is``" test will fail.
84-    Prefer an equality test (using "``==``") in cases like this.
85-
86 ``CharField``
87 -------------
88 
89diff --git a/docs/releases/1.2.txt b/docs/releases/1.2.txt
90index b952ec1..703ded3 100644
91--- a/docs/releases/1.2.txt
92+++ b/docs/releases/1.2.txt
93@@ -302,6 +302,16 @@ created using ``decorator_from_middleware``.
94 
95 .. _deprecated-features-1.2:
96 
97+``BooleanField`` on MySQL
98+--------------------------
99+
100+In previous versions of Django ``BoleanFields`` under MySQL would return their
101+values as either ``1`` or ``0``, instead of ``True`` or ``False``.  For most
102+people this shouldn't have been a problem because ``bool`` is a subclass of
103+``int``, however in Django 1.2 MySQL correctly returns a real ``bool``.  The
104+only time this should ever be an issue is if you were expecting printing the
105+``repr`` of a ``BooleanField`` to print ``1`` or ``0``.
106+
107 Features deprecated in 1.2
108 ==========================
109 
110diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
111index 18bfbd3..1ee92d4 100644
112--- a/tests/regressiontests/model_fields/tests.py
113+++ b/tests/regressiontests/model_fields/tests.py
114@@ -107,12 +107,22 @@ class BooleanFieldTests(unittest.TestCase):
115         self.assertEqual(f.get_db_prep_lookup('exact', '0', connection=connection), [False])
116         self.assertEqual(f.get_db_prep_lookup('exact', 0, connection=connection), [False])
117         self.assertEqual(f.get_db_prep_lookup('exact', None, connection=connection), [None])
118+   
119+    def _test_to_python(self, f):
120+        self.assertTrue(f.to_python(1) is True)
121+        self.assertTrue(f.to_python(0) is False)
122 
123     def test_booleanfield_get_db_prep_lookup(self):
124         self._test_get_db_prep_lookup(models.BooleanField())
125 
126     def test_nullbooleanfield_get_db_prep_lookup(self):
127         self._test_get_db_prep_lookup(models.NullBooleanField())
128+   
129+    def test_booleanfield_to_python(self):
130+        self._test_to_python(models.BooleanField())
131+   
132+    def test_nullbooleanfield_to_python(self):
133+        self._test_to_python(models.NullBooleanField())
134 
135     def test_booleanfield_choices_blank(self):
136         """