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):
|
513 | 513 | |
514 | 514 | def to_python(self, value): |
515 | 515 | 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 |
517 | 517 | # to return a true bool for semantic reasons. |
518 | 518 | return bool(value) |
519 | | if value in ('t', 'True', '1'): |
| 519 | if value in ('t', 'True', '1', 'on'): |
520 | 520 | return True |
521 | | if value in ('f', 'False', '0'): |
| 521 | if value in ('f', 'False', '0', 'off'): |
522 | 522 | return False |
523 | 523 | raise exceptions.ValidationError(self.error_messages['invalid']) |
524 | 524 | |
… |
… |
class NullBooleanField(Field):
|
999 | 999 | return bool(value) |
1000 | 1000 | if value in ('None',): |
1001 | 1001 | return None |
1002 | | if value in ('t', 'True', '1'): |
| 1002 | if value in ('t', 'True', '1', 'on'): |
1003 | 1003 | return True |
1004 | | if value in ('f', 'False', '0'): |
| 1004 | if value in ('f', 'False', '0', 'off'): |
1005 | 1005 | return False |
1006 | 1006 | raise exceptions.ValidationError(self.error_messages['invalid']) |
1007 | 1007 | |
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):
|
143 | 143 | self.assertTrue(f.to_python(1) is True) |
144 | 144 | self.assertTrue(f.to_python(0) is False) |
145 | 145 | |
| 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 | |
146 | 150 | def test_booleanfield_get_db_prep_lookup(self): |
147 | 151 | self._test_get_db_prep_lookup(models.BooleanField()) |
148 | 152 | |
… |
… |
class BooleanFieldTests(unittest.TestCase):
|
151 | 155 | |
152 | 156 | def test_booleanfield_to_python(self): |
153 | 157 | self._test_to_python(models.BooleanField()) |
| 158 | self._test_on_off_to_python(models.BooleanField()) |
154 | 159 | |
155 | 160 | def test_nullbooleanfield_to_python(self): |
156 | 161 | self._test_to_python(models.NullBooleanField()) |
| 162 | self._test_on_off_to_python(models.NullBooleanField()) |
157 | 163 | |
158 | 164 | def test_booleanfield_choices_blank(self): |
159 | 165 | """ |