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

File django-real-bools.2.diff, 4.4 KB (added by Alex Gaynor, 15 years ago)
  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 05f16db..02c69c2 100644
    a b class BooleanField(Field):  
    524524        return "BooleanField"
    525525
    526526    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
    530535        raise exceptions.ValidationError(self.error_messages['invalid'])
    531536
    532537    def get_prep_lookup(self, lookup_type, value):
    class NullBooleanField(Field):  
    934939        return "NullBooleanField"
    935940
    936941    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
    941952        raise exceptions.ValidationError(self.error_messages['invalid'])
    942953
    943954    def get_prep_lookup(self, lookup_type, value):
  • docs/ref/databases.txt

    diff --git a/docs/ref/databases.txt b/docs/ref/databases.txt
    index afead83..3aef61c 100644
    a b storage engine, you have a couple of options.  
    323323Notes on specific fields
    324324------------------------
    325325
    326 Boolean fields
    327 ~~~~~~~~~~~~~~
    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.``.
    335 
    336326Character fields
    337327~~~~~~~~~~~~~~~~
    338328
  • docs/releases/1.2.txt

    diff --git a/docs/releases/1.2.txt b/docs/releases/1.2.txt
    index b952ec1..703ded3 100644
    a b created using ``decorator_from_middleware``.  
    302302
    303303.. _deprecated-features-1.2:
    304304
     305``BooleanField`` on MySQL
     306--------------------------
     307
     308In previous versions of Django ``BoleanFields`` under MySQL would return their
     309values as either ``1`` or ``0``, instead of ``True`` or ``False``.  For most
     310people 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
     312only 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
    305315Features deprecated in 1.2
    306316==========================
    307317
  • tests/regressiontests/model_fields/tests.py

    diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
    index 18bfbd3..1ee92d4 100644
    a b class BooleanFieldTests(unittest.TestCase):  
    107107        self.assertEqual(f.get_db_prep_lookup('exact', '0', connection=connection), [False])
    108108        self.assertEqual(f.get_db_prep_lookup('exact', 0, connection=connection), [False])
    109109        self.assertEqual(f.get_db_prep_lookup('exact', None, connection=connection), [None])
     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)
    110114
    111115    def test_booleanfield_get_db_prep_lookup(self):
    112116        self._test_get_db_prep_lookup(models.BooleanField())
    113117
    114118    def test_nullbooleanfield_get_db_prep_lookup(self):
    115119        self._test_get_db_prep_lookup(models.NullBooleanField())
     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())
    116126
    117127    def test_booleanfield_choices_blank(self):
    118128        """
Back to Top