Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7190. Boolean fields now return bool values instead of 1 or 0.…

… Thanks, Alex Gaynor.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12578 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 16fe73d918c8a35f7122aed858312bd3163ac454 1 parent 9f4bf52
Joseph Kocherhans authored February 24, 2010
25  django/db/models/fields/__init__.py
@@ -524,9 +524,14 @@ def get_internal_type(self):
524 524
         return "BooleanField"
525 525
 
526 526
     def to_python(self, value):
527  
-        if value in (True, False): return value
528  
-        if value in ('t', 'True', '1'): return True
529  
-        if value in ('f', 'False', '0'): return False
  527
+        if value in (True, False):
  528
+            # if value is 1 or 0 than it's equal to True or False, but we want
  529
+            # to return a true bool for semantic reasons.
  530
+            return bool(value)
  531
+        if value in ('t', 'True', '1'):
  532
+            return True
  533
+        if value in ('f', 'False', '0'):
  534
+            return False
530 535
         raise exceptions.ValidationError(self.error_messages['invalid'])
531 536
 
532 537
     def get_prep_lookup(self, lookup_type, value):
@@ -934,10 +939,16 @@ def get_internal_type(self):
934 939
         return "NullBooleanField"
935 940
 
936 941
     def to_python(self, value):
937  
-        if value in (None, True, False): return value
938  
-        if value in ('None',): return None
939  
-        if value in ('t', 'True', '1'): return True
940  
-        if value in ('f', 'False', '0'): return False
  942
+        if value is None:
  943
+            return None
  944
+        if value in (True, False):
  945
+            return bool(value)
  946
+        if value in ('None',):
  947
+            return None
  948
+        if value in ('t', 'True', '1'):
  949
+            return True
  950
+        if value in ('f', 'False', '0'):
  951
+            return False
941 952
         raise exceptions.ValidationError(self.error_messages['invalid'])
942 953
 
943 954
     def get_prep_lookup(self, lookup_type, value):
11  docs/ref/databases.txt
@@ -326,12 +326,11 @@ Notes on specific fields
326 326
 Boolean fields
327 327
 ~~~~~~~~~~~~~~
328 328
 
329  
-Since MySQL doesn't have a direct ``BOOLEAN`` column type, Django uses a
330  
-``TINYINT`` column with values of ``1`` and ``0`` to store values for the
331  
-:class:`~django.db.models.BooleanField` model field. Refer to the documentation
332  
-of that field for more details, but usually this won't be something that will
333  
-matter unless you're printing out the field values and are expecting to see
334  
-``True`` and ``False.``.
  329
+.. versionchanged:: 1.2
  330
+
  331
+In previous versions of Django when running under MySQL ``BooleanFields`` would
  332
+return their data as ``ints``, instead of true ``bools``.  See the release
  333
+notes for a complete description of the change.
335 334
 
336 335
 Character fields
337 336
 ~~~~~~~~~~~~~~~~
17  docs/ref/models/fields.txt
@@ -342,18 +342,11 @@ A true/false field.
342 342
 
343 343
 The admin represents this as a checkbox.
344 344
 
345  
-.. admonition:: MySQL users..
346  
-
347  
-    A boolean field in MySQL is stored as a ``TINYINT`` column with a value of
348  
-    either 0 or 1 (most databases have a proper ``BOOLEAN`` type instead). So,
349  
-    for MySQL, only, when a ``BooleanField`` is retrieved from the database
350  
-    and stored on a model attribute, it will have the values 1 or 0, rather
351  
-    than ``True`` or ``False``. Normally, this shouldn't be a problem, since
352  
-    Python guarantees that ``1 == True`` and ``0 == False`` are both true.
353  
-    Just be careful if you're writing something like ``obj is True`` when
354  
-    ``obj`` is a value from a boolean attribute on a model. If that model was
355  
-    constructed using the ``mysql`` backend, the "``is``" test will fail.
356  
-    Prefer an equality test (using "``==``") in cases like this.
  345
+.. versionchanged:: 1.2
  346
+
  347
+    In previous versions of Django when running under MySQL ``BooleanFields``
  348
+    would return their data as ``ints``, instead of true ``bools``.  See the
  349
+    release notes for a complete description of the change.
357 350
 
358 351
 ``CharField``
359 352
 -------------
10  docs/releases/1.2.txt
@@ -302,6 +302,16 @@ created using ``decorator_from_middleware``.
302 302
 
303 303
 .. _deprecated-features-1.2:
304 304
 
  305
+``BooleanField`` on MySQL
  306
+--------------------------
  307
+
  308
+In previous versions of Django ``BoleanFields`` under MySQL would return their
  309
+values as either ``1`` or ``0``, instead of ``True`` or ``False``.  For most
  310
+people this shouldn't have been a problem because ``bool`` is a subclass of
  311
+``int``, however in Django 1.2 MySQL correctly returns a real ``bool``.  The
  312
+only time this should ever be an issue is if you were expecting printing the
  313
+``repr`` of a ``BooleanField`` to print ``1`` or ``0``.
  314
+
305 315
 Features deprecated in 1.2
306 316
 ==========================
307 317
 
10  tests/regressiontests/model_fields/tests.py
@@ -108,12 +108,22 @@ def _test_get_db_prep_lookup(self, f):
108 108
         self.assertEqual(f.get_db_prep_lookup('exact', 0, connection=connection), [False])
109 109
         self.assertEqual(f.get_db_prep_lookup('exact', None, connection=connection), [None])
110 110
 
  111
+    def _test_to_python(self, f):
  112
+        self.assertTrue(f.to_python(1) is True)
  113
+        self.assertTrue(f.to_python(0) is False)
  114
+
111 115
     def test_booleanfield_get_db_prep_lookup(self):
112 116
         self._test_get_db_prep_lookup(models.BooleanField())
113 117
 
114 118
     def test_nullbooleanfield_get_db_prep_lookup(self):
115 119
         self._test_get_db_prep_lookup(models.NullBooleanField())
116 120
 
  121
+    def test_booleanfield_to_python(self):
  122
+        self._test_to_python(models.BooleanField())
  123
+
  124
+    def test_nullbooleanfield_to_python(self):
  125
+        self._test_to_python(models.NullBooleanField())
  126
+
117 127
     def test_booleanfield_choices_blank(self):
118 128
         """
119 129
         Test that BooleanField with choices and defaults doesn't generate a

0 notes on commit 16fe73d

Please sign in to comment.
Something went wrong with that request. Please try again.