Ticket #15497: 15497.diff

File 15497.diff, 2.8 KB (added by Julie Pichon, 8 years ago)
  • django/db/models/fields/__init__.py

    From c572276a4ee8c85020e6aae69a7d01fe5efb10b1 Mon Sep 17 00:00:00 2001
    From: Julie Pichon <julie@jpichon.net>
    Date: Sat, 25 Jun 2011 15:52:13 +0100
    Subject: [PATCH] 15497: Isaffre's patch + NullBooleanField + tests
    
    ---
     django/db/models/fields/__init__.py         |   10 +++++-----
     tests/regressiontests/model_fields/tests.py |    6 ++++++
     2 files changed, 11 insertions(+), 5 deletions(-)
    
    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 91ecbf2..6b617a0 100644
    a b class BooleanField(Field): 
    513513
    514514    def to_python(self, value):
    515515        if value in (True, False):
    516             # if value is 1 or 0 than it's equal to True or False, but we want
     516            # if value is 1 or 0 then it's equal to True or False, but we want
    517517            # to return a true bool for semantic reasons.
    518518            return bool(value)
    519         if value in ('t', 'True', '1'):
     519        if value in ('t', 'True', '1', 'on'):
    520520            return True
    521         if value in ('f', 'False', '0'):
     521        if value in ('f', 'False', '0', 'off'):
    522522            return False
    523523        raise exceptions.ValidationError(self.error_messages['invalid'])
    524524
    class NullBooleanField(Field): 
    999999            return bool(value)
    10001000        if value in ('None',):
    10011001            return None
    1002         if value in ('t', 'True', '1'):
     1002        if value in ('t', 'True', '1', 'on'):
    10031003            return True
    1004         if value in ('f', 'False', '0'):
     1004        if value in ('f', 'False', '0', 'off'):
    10051005            return False
    10061006        raise exceptions.ValidationError(self.error_messages['invalid'])
    10071007
  • tests/regressiontests/model_fields/tests.py

    diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
    index b42c0af..6c4155f 100644
    a b class BooleanFieldTests(unittest.TestCase): 
    143143        self.assertTrue(f.to_python(1) is True)
    144144        self.assertTrue(f.to_python(0) is False)
    145145
     146    def _test_on_off_to_python(self, f):
     147        self.assertTrue(f.to_python('on') is True)
     148        self.assertTrue(f.to_python('off') is False)
     149
    146150    def test_booleanfield_get_db_prep_lookup(self):
    147151        self._test_get_db_prep_lookup(models.BooleanField())
    148152
    class BooleanFieldTests(unittest.TestCase): 
    151155
    152156    def test_booleanfield_to_python(self):
    153157        self._test_to_python(models.BooleanField())
     158        self._test_on_off_to_python(models.BooleanField())
    154159
    155160    def test_nullbooleanfield_to_python(self):
    156161        self._test_to_python(models.NullBooleanField())
     162        self._test_on_off_to_python(models.NullBooleanField())
    157163
    158164    def test_booleanfield_choices_blank(self):
    159165        """
Back to Top