Django

Code

Ticket #4485: decimalfield_null.2.diff

File decimalfield_null.2.diff, 1.2 kB (added by tdterry, 1 year ago)

patch to allow DecimalField? to contain NULL; regression tests included

  • django/db/models/fields/__init__.py

    old new  
    634634                _("This value must be a decimal number.")) 
    635635 
    636636    def _format(self, value): 
    637         if isinstance(value, basestring)
     637        if isinstance(value, basestring) or value is None
    638638            return value 
    639639        else: 
    640640            return self.format_number(value) 
  • tests/regressiontests/model_fields/tests.py

    old new  
    1515Traceback (most recent call last): 
    1616... 
    1717ValidationError: [u'This value must be a decimal number.'] 
     18 
     19>>> f = DecimalField(max_digits=5, decimal_places=1) 
     20 
     21>>> f.get_db_prep_save(2.0) 
     22u'2.0' 
     23 
     24>>> f.get_db_prep_save(2.56) 
     25u'2.6' 
     26 
     27>>> f.get_db_prep_save(None) 
     28 
     29>>> f.get_db_prep_lookup('exact', 2.0) 
     30[u'2.0'] 
     31 
     32>>> f.get_db_prep_lookup('exact', 2.56) 
     33[u'2.6'] 
     34 
     35>>> f.get_db_prep_lookup('exact', None) 
     36[None] 
    1837"""