Ticket #9590: char-text-field-null-value-save-null.patch

File char-text-field-null-value-save-null.patch, 3.2 KB (added by romke, 6 years ago)

patch + tests

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

     
    414414
    415415    def formfield(self, **kwargs):
    416416        defaults = {'max_length': self.max_length}
     417        if self.null:
     418            defaults['empty_value'] = None
    417419        defaults.update(kwargs)
    418420        return super(CharField, self).formfield(**defaults)
    419421
     
    785787
    786788    def formfield(self, **kwargs):
    787789        defaults = {'widget': forms.Textarea}
     790        if self.null:
     791            defaults['empty_value'] = None
    788792        defaults.update(kwargs)
    789793        return super(TextField, self).formfield(**defaults)
    790794
  • django/forms/fields.py

     
    139139        'min_length': _(u'Ensure this value has at least %(min)d characters (it has %(length)d).'),
    140140    }
    141141
    142     def __init__(self, max_length=None, min_length=None, *args, **kwargs):
     142    def __init__(self, max_length=None, min_length=None, empty_value=u'', *args, **kwargs):
    143143        self.max_length, self.min_length = max_length, min_length
     144        self.empty_value = empty_value
    144145        super(CharField, self).__init__(*args, **kwargs)
    145146
    146147    def clean(self, value):
    147148        "Validates max_length and min_length. Returns a Unicode object."
    148149        super(CharField, self).clean(value)
    149150        if value in EMPTY_VALUES:
    150             return u''
     151            return self.empty_value
    151152        value = smart_unicode(value)
    152153        value_length = len(value)
    153154        if self.max_length is not None and value_length > self.max_length:
  • tests/modeltests/model_forms/models.py

     
    168168    def __unicode__(self):
    169169        return self.key
    170170
     171class NullCharField(models.Model):
     172    testfield = models.CharField(max_length=20, blank=True, null=True)
     173    testfield2 = models.TextField(null=True, blank=True)
     174    def __unicode__(self):
     175        return unicode(self.testfield)
     176
    171177__test__ = {'API_TESTS': """
    172178>>> from django import forms
    173179>>> from django.forms.models import ModelForm, model_to_dict
     
    12771283>>> form.errors
    12781284{'__all__': [u'Explicit pk with this Key and Desc already exists.'], 'key': [u'Explicit pk with this Key already exists.'], 'desc': [u'Explicit pk with this Desc already exists.']}
    12791285
     1286# Ensure that char field with blank=True, null=True will be inserted as NULL not as ''
     1287>>> class NullCharFieldForm(ModelForm):
     1288...     class Meta:
     1289...         model = NullCharField
     1290...         fields = ('testfield', 'testfield2',)
     1291>>> form = NullCharFieldForm({'testfield': '', 'testfield2': ''})
     1292>>> form.is_valid()
     1293True
     1294>>> obj = form.save()
     1295>>> obj.testfield is None
     1296True
     1297>>> obj.testfield2 is None
     1298True
     1299>>> obj = NullCharField.objects.all()[0]
     1300>>> obj.testfield is None
     1301True
     1302>>> obj.testfield2 is None
     1303True
     1304
    12801305# Choices on CharField and IntegerField
    12811306>>> class ArticleForm(ModelForm):
    12821307...     class Meta:
Back to Top