From 1ce1f387b031c368e0ad315964b78f93d6be9a19 Mon Sep 17 00:00:00 2001 From: Serhiy Voyt Date: Tue, 6 May 2014 21:57:25 +0300 Subject: [PATCH] Charfied from_native method returns default instead of None. Updated tests. --- rest_framework/fields.py | 9 ++++++++- rest_framework/tests/models.py | 4 +++- rest_framework/tests/test_serializer.py | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 8cdc55515..7858d9510 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -469,8 +469,15 @@ 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: + if self.default: + return self.default + else: + value + return smart_text(value) diff --git a/rest_framework/tests/models.py b/rest_framework/tests/models.py index e171d3bd9..fba3f8f7c 100644 --- a/rest_framework/tests/models.py +++ b/rest_framework/tests/models.py @@ -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 diff --git a/rest_framework/tests/test_serializer.py b/rest_framework/tests/test_serializer.py index d770c6375..82e1a89ca 100644 --- a/rest_framework/tests/test_serializer.py +++ b/rest_framework/tests/test_serializer.py @@ -1238,6 +1238,7 @@ class BlankFieldTests(TestCase): self.assertEqual(serializer.is_valid(), True) serializer.save() self.assertTrue(serializer.object.pk is not None) + self.assertEqual(serializer.object.title, 'title') def test_create_not_blank_field(self): """