1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

Fixed #4136 -- Made ModelForm save empty values for nullable CharFields as NULL.

Previously, empty values were saved as strings.
This commit is contained in:
Jon Dufresne
2016-05-18 07:30:42 -07:00
committed by Tim Graham
parent f2c0eb19e9
commit 267dc4addd
9 changed files with 66 additions and 18 deletions

View File

@@ -214,10 +214,11 @@ class Field(object):
class CharField(Field):
def __init__(self, max_length=None, min_length=None, strip=True, *args, **kwargs):
def __init__(self, max_length=None, min_length=None, strip=True, empty_value='', *args, **kwargs):
self.max_length = max_length
self.min_length = min_length
self.strip = strip
self.empty_value = empty_value
super(CharField, self).__init__(*args, **kwargs)
if min_length is not None:
self.validators.append(validators.MinLengthValidator(int(min_length)))
@@ -227,7 +228,7 @@ class CharField(Field):
def to_python(self, value):
"Returns a Unicode object."
if value in self.empty_values:
return ''
return self.empty_value
value = force_text(value)
if self.strip:
value = value.strip()