Merge pull request #1665 from smal/modelserialization-charfield-with-null

Modelserialization charfield with null
This commit is contained in:
Tom Christie 2014-06-27 16:39:54 +01:00
commit 8eadac39ad
3 changed files with 11 additions and 2 deletions

View File

@ -474,8 +474,12 @@ class CharField(WritableField):
self.validators.append(validators.MaxLengthValidator(max_length))
def from_native(self, value):
if isinstance(value, six.string_types) or value is None:
if isinstance(value, six.string_types):
return value
if value is None:
return ''
return smart_text(value)

View File

@ -105,6 +105,7 @@ class Album(RESTFrameworkModel):
title = models.CharField(max_length=100, unique=True)
ref = models.CharField(max_length=10, unique=True, null=True, blank=True)
class Photo(RESTFrameworkModel):
description = models.TextField()
album = models.ForeignKey(Album)
@ -112,7 +113,8 @@ class Photo(RESTFrameworkModel):
# Model for issue #324
class BlankFieldModel(RESTFrameworkModel):
title = models.CharField(max_length=100, blank=True, null=False)
title = models.CharField(max_length=100, blank=True, null=False,
default="title")
# Model for issue #380

View File

@ -1236,6 +1236,9 @@ class BlankFieldTests(TestCase):
def test_create_model_null_field(self):
serializer = self.model_serializer_class(data={'title': None})
self.assertEqual(serializer.is_valid(), True)
serializer.save()
self.assertIsNot(serializer.object.pk, None)
self.assertEqual(serializer.object.title, '')
def test_create_not_blank_field(self):
"""