Ticket #9590: char-text-field-null-value-save-null.patch
File char-text-field-null-value-save-null.patch, 3.2 KB (added by , 16 years ago) |
---|
-
django/db/models/fields/__init__.py
414 414 415 415 def formfield(self, **kwargs): 416 416 defaults = {'max_length': self.max_length} 417 if self.null: 418 defaults['empty_value'] = None 417 419 defaults.update(kwargs) 418 420 return super(CharField, self).formfield(**defaults) 419 421 … … 785 787 786 788 def formfield(self, **kwargs): 787 789 defaults = {'widget': forms.Textarea} 790 if self.null: 791 defaults['empty_value'] = None 788 792 defaults.update(kwargs) 789 793 return super(TextField, self).formfield(**defaults) 790 794 -
django/forms/fields.py
139 139 'min_length': _(u'Ensure this value has at least %(min)d characters (it has %(length)d).'), 140 140 } 141 141 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): 143 143 self.max_length, self.min_length = max_length, min_length 144 self.empty_value = empty_value 144 145 super(CharField, self).__init__(*args, **kwargs) 145 146 146 147 def clean(self, value): 147 148 "Validates max_length and min_length. Returns a Unicode object." 148 149 super(CharField, self).clean(value) 149 150 if value in EMPTY_VALUES: 150 return u''151 return self.empty_value 151 152 value = smart_unicode(value) 152 153 value_length = len(value) 153 154 if self.max_length is not None and value_length > self.max_length: -
tests/modeltests/model_forms/models.py
168 168 def __unicode__(self): 169 169 return self.key 170 170 171 class 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 171 177 __test__ = {'API_TESTS': """ 172 178 >>> from django import forms 173 179 >>> from django.forms.models import ModelForm, model_to_dict … … 1277 1283 >>> form.errors 1278 1284 {'__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.']} 1279 1285 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() 1293 True 1294 >>> obj = form.save() 1295 >>> obj.testfield is None 1296 True 1297 >>> obj.testfield2 is None 1298 True 1299 >>> obj = NullCharField.objects.all()[0] 1300 >>> obj.testfield is None 1301 True 1302 >>> obj.testfield2 is None 1303 True 1304 1280 1305 # Choices on CharField and IntegerField 1281 1306 >>> class ArticleForm(ModelForm): 1282 1307 ... class Meta: