#356 Fixes issue with blank=True in related fields if related field is defined in serializer. Does not work for those from model.

This commit is contained in:
Ian Strachan 2012-11-04 21:39:17 +00:00
parent d81ded1157
commit c5dfec8513
2 changed files with 15 additions and 2 deletions

View File

@ -313,7 +313,10 @@ class RelatedField(WritableField):
return return
value = data.get(field_name) value = data.get(field_name)
into[(self.source or field_name)] = self.from_native(value) if self.blank and value is None:
into[(self.source or field_name)] = None
else:
into[(self.source or field_name)] = self.from_native(value)
class ManyRelatedMixin(object): class ManyRelatedMixin(object):

View File

@ -3,7 +3,7 @@ from django.test import TestCase
from rest_framework import serializers from rest_framework import serializers
from rest_framework.tests.models import (ActionItem, Anchor, BasicModel, from rest_framework.tests.models import (ActionItem, Anchor, BasicModel,
BlankFieldModel, BlogPost, CallableDefaultValueModel, DefaultValueModel, BlankFieldModel, BlogPost, CallableDefaultValueModel, DefaultValueModel,
ManyToManyModel, Person, ReadOnlyManyToManyModel) ManyToManyModel, Person, ReadOnlyManyToManyModel, BlankRelatedFieldModel)
class SubComment(object): class SubComment(object):
@ -525,13 +525,23 @@ class BlankFieldTests(TestCase):
class BlankRelatedFieldModelTests(TestCase): class BlankRelatedFieldModelTests(TestCase):
def setUp(self): def setUp(self):
class BlankInlineRelatedFieldModelSerializer(serializers.ModelSerializer):
blank_field = serializers.PrimaryKeyRelatedField(blank=True, queryset=BasicModel.objects.all())
class Meta:
model = BlankRelatedFieldModel
class BlankRelatedFieldModelSerializer(serializers.ModelSerializer): class BlankRelatedFieldModelSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = BlankRelatedFieldModel model = BlankRelatedFieldModel
self.inline_serializer_class = BlankInlineRelatedFieldModelSerializer
self.serializer_class = BlankRelatedFieldModelSerializer self.serializer_class = BlankRelatedFieldModelSerializer
self.data = {'blank_field': None} self.data = {'blank_field': None}
def test_create_inline_blank_related_field(self):
serializer = self.inline_serializer_class(self.data)
self.assertEquals(serializer.is_valid(), True)
def test_create_blank_related_field(self): def test_create_blank_related_field(self):
serializer = self.serializer_class(self.data) serializer = self.serializer_class(self.data)
self.assertEquals(serializer.is_valid(), True) self.assertEquals(serializer.is_valid(), True)