mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-12-03 23:14:07 +03:00
#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:
parent
d81ded1157
commit
c5dfec8513
|
@ -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):
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user