mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-11 04:07:39 +03:00
Merge pull request #1665 from smal/modelserialization-charfield-with-null
Modelserialization charfield with null
This commit is contained in:
commit
8eadac39ad
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user