summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/db/models/fields/__init__.py18
-rw-r--r--tests/regressiontests/model_fields/tests.py26
2 files changed, 44 insertions, 0 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index d4f3c6f344..4ddf901ea5 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -362,6 +362,15 @@ class BooleanField(Field):
raise exceptions.ValidationError(
_("This value must be either True or False."))
+ def get_db_prep_lookup(self, lookup_type, value):
+ # Special-case handling for filters coming from a web request (e.g. the
+ # admin interface). Only works for scalar values (not lists). If you're
+ # passing in a list, you might as well make things the right type when
+ # constructing the list.
+ if value in ('1', '0'):
+ value = bool(int(value))
+ return super(BooleanField, self).get_db_prep_lookup(lookup_type, value)
+
def get_db_prep_value(self, value):
if value is None:
return None
@@ -693,6 +702,15 @@ class NullBooleanField(Field):
raise exceptions.ValidationError(
_("This value must be either None, True or False."))
+ def get_db_prep_lookup(self, lookup_type, value):
+ # Special-case handling for filters coming from a web request (e.g. the
+ # admin interface). Only works for scalar values (not lists). If you're
+ # passing in a list, you might as well make things the right type when
+ # constructing the list.
+ if value in ('1', '0'):
+ value = bool(int(value))
+ return super(NullBooleanField, self).get_db_prep_lookup(lookup_type, value)
+
def get_db_prep_value(self, value):
if value is None:
return None
diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
index 0e67c60ac9..80ff4bada6 100644
--- a/tests/regressiontests/model_fields/tests.py
+++ b/tests/regressiontests/model_fields/tests.py
@@ -45,5 +45,31 @@ datetime.time(1, 2, 3, 4)
>>> f.to_python('01:02:03.999999')
datetime.time(1, 2, 3, 999999)
+# Boolean and null boolean fields
+>>> f = BooleanField()
+>>> for val in (True, '1', 1):
+... f.get_db_prep_lookup('exact', val)
+[True]
+[True]
+[True]
+>>> for val in (False, '0', 0):
+... f.get_db_prep_lookup('exact', val)
+[False]
+[False]
+[False]
+
+>>> f = NullBooleanField()
+>>> for val in (True, '1', 1):
+... f.get_db_prep_lookup('exact', val)
+[True]
+[True]
+[True]
+>>> for val in (False, '0', 0):
+... f.get_db_prep_lookup('exact', val)
+[False]
+[False]
+[False]
+>>> f.get_db_prep_lookup('exact', None)
+[None]
"""