Author: mtredinnick
Date: 2008-08-28 21:40:56 -0500 (Thu, 28 Aug 2008)
New Revision: 8691
Modified:
django/trunk/django/db/models/fields/__init__.py
django/trunk/tests/regressiontests/model_fields/tests.py
Log:
Fixed #8101 -- Allow the strings '1' and '0' as filter values for boolean
fields (the latter was causing problems). This allows these values in URLs
(e.g. the admin filtering).
Not an ideal solution to the problem, but will do the job for the time being.
Modified: django/trunk/django/db/models/fields/__init__.py
===================================================================
--- django/trunk/django/db/models/fields/__init__.py 2008-08-29 02:40:50 UTC
(rev 8690)
+++ django/trunk/django/db/models/fields/__init__.py 2008-08-29 02:40:56 UTC
(rev 8691)
@@ -362,6 +362,15 @@
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 @@
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
Modified: django/trunk/tests/regressiontests/model_fields/tests.py
===================================================================
--- django/trunk/tests/regressiontests/model_fields/tests.py 2008-08-29
02:40:50 UTC (rev 8690)
+++ django/trunk/tests/regressiontests/model_fields/tests.py 2008-08-29
02:40:56 UTC (rev 8691)
@@ -45,5 +45,31 @@
>>> 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]
+
"""
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---