Django

Code

Changeset 8691

Show
Ignore:
Timestamp:
08/28/08 21:40:56 (3 months ago)
Author:
mtredinnick
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/fields/__init__.py

    r8684 r8691  
    363363            _("This value must be either True or False.")) 
    364364 
     365    def get_db_prep_lookup(self, lookup_type, value): 
     366        # Special-case handling for filters coming from a web request (e.g. the 
     367        # admin interface). Only works for scalar values (not lists). If you're 
     368        # passing in a list, you might as well make things the right type when 
     369        # constructing the list. 
     370        if value in ('1', '0'): 
     371            value = bool(int(value)) 
     372        return super(BooleanField, self).get_db_prep_lookup(lookup_type, value) 
     373 
    365374    def get_db_prep_value(self, value): 
    366375        if value is None: 
     
    694703            _("This value must be either None, True or False.")) 
    695704 
     705    def get_db_prep_lookup(self, lookup_type, value): 
     706        # Special-case handling for filters coming from a web request (e.g. the 
     707        # admin interface). Only works for scalar values (not lists). If you're 
     708        # passing in a list, you might as well make things the right type when 
     709        # constructing the list. 
     710        if value in ('1', '0'): 
     711            value = bool(int(value)) 
     712        return super(NullBooleanField, self).get_db_prep_lookup(lookup_type, value) 
     713 
    696714    def get_db_prep_value(self, value): 
    697715        if value is None: 
  • django/trunk/tests/regressiontests/model_fields/tests.py

    r8616 r8691  
    4646datetime.time(1, 2, 3, 999999) 
    4747 
     48# Boolean and null boolean fields 
     49>>> f = BooleanField() 
     50>>> for val in (True, '1', 1): 
     51...     f.get_db_prep_lookup('exact', val) 
     52[True] 
     53[True] 
     54[True] 
     55>>> for val in (False, '0', 0): 
     56...     f.get_db_prep_lookup('exact', val) 
     57[False] 
     58[False] 
     59[False] 
     60 
     61>>> f = NullBooleanField() 
     62>>> for val in (True, '1', 1): 
     63...     f.get_db_prep_lookup('exact', val) 
     64[True] 
     65[True] 
     66[True] 
     67>>> for val in (False, '0', 0): 
     68...     f.get_db_prep_lookup('exact', val) 
     69[False] 
     70[False] 
     71[False] 
     72>>> f.get_db_prep_lookup('exact', None) 
     73[None] 
    4874 
    4975"""