mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed #4485 -- Allow nullable DecimalFields to store NULLs.
Based on a patch from tdterry. Thanks. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7797 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
		@@ -695,7 +695,7 @@ class DecimalField(Field):
 | 
				
			|||||||
                _("This value must be a decimal number."))
 | 
					                _("This value must be a decimal number."))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _format(self, value):
 | 
					    def _format(self, value):
 | 
				
			||||||
        if isinstance(value, basestring):
 | 
					        if isinstance(value, basestring) or value is None:
 | 
				
			||||||
            return value
 | 
					            return value
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            return self.format_number(value)
 | 
					            return self.format_number(value)
 | 
				
			||||||
@@ -716,8 +716,7 @@ class DecimalField(Field):
 | 
				
			|||||||
        return u"%.*f" % (self.decimal_places, value)
 | 
					        return u"%.*f" % (self.decimal_places, value)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_db_prep_save(self, value):
 | 
					    def get_db_prep_save(self, value):
 | 
				
			||||||
        if value is not None:
 | 
					        value = self._format(value)
 | 
				
			||||||
            value = self._format(value)
 | 
					 | 
				
			||||||
        return super(DecimalField, self).get_db_prep_save(value)
 | 
					        return super(DecimalField, self).get_db_prep_save(value)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_db_prep_lookup(self, lookup_type, value):
 | 
					    def get_db_prep_lookup(self, lookup_type, value):
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,4 +15,21 @@ Decimal("3.14")
 | 
				
			|||||||
Traceback (most recent call last):
 | 
					Traceback (most recent call last):
 | 
				
			||||||
...
 | 
					...
 | 
				
			||||||
ValidationError: [u'This value must be a decimal number.']
 | 
					ValidationError: [u'This value must be a decimal number.']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					>>> f = DecimalField(max_digits=5, decimal_places=1)
 | 
				
			||||||
 | 
					>>> x = f.to_python(2)
 | 
				
			||||||
 | 
					>>> y = f.to_python('2.6')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					>>> f.get_db_prep_save(x)
 | 
				
			||||||
 | 
					u'2.0'
 | 
				
			||||||
 | 
					>>> f.get_db_prep_save(y)
 | 
				
			||||||
 | 
					u'2.6'
 | 
				
			||||||
 | 
					>>> f.get_db_prep_save(None)
 | 
				
			||||||
 | 
					>>> f.get_db_prep_lookup('exact', x)
 | 
				
			||||||
 | 
					[u'2.0']
 | 
				
			||||||
 | 
					>>> f.get_db_prep_lookup('exact', y)
 | 
				
			||||||
 | 
					[u'2.6']
 | 
				
			||||||
 | 
					>>> f.get_db_prep_lookup('exact', None)
 | 
				
			||||||
 | 
					[None]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
"""
 | 
					"""
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user