#5848 Allow traversing nullable related fields (#5849)

This commit is contained in:
George-Cristian Bîrzan 2018-03-20 23:02:22 +02:00 committed by Carlton Gibson
parent 6c0c69ed65
commit 85c588b0c1
2 changed files with 22 additions and 1 deletions

View File

@ -174,7 +174,7 @@ class RelatedField(Field):
pass
# Standard case, return the object instance.
return get_attribute(instance, self.source_attrs)
return super(RelatedField, self).get_attribute(instance)
def get_choices(self, cutoff=None):
queryset = self.get_queryset()

View File

@ -23,6 +23,8 @@ from django.utils import six
from rest_framework import serializers
from rest_framework.compat import postgres_fields, unicode_repr
from .models import NestedForeignKeySource
def dedent(blocktext):
return '\n'.join([line[12:] for line in blocktext.splitlines()[1:-1]])
@ -1164,6 +1166,25 @@ class Test5004UniqueChoiceField(TestCase):
class TestFieldSource(TestCase):
def test_traverse_nullable_fk(self):
"""
A dotted source with nullable elements uses default when any item in the chain is None. #5849.
Similar to model example from test_serializer.py `test_default_for_multiple_dotted_source` method,
but using RelatedField, rather than CharField.
"""
class TestSerializer(serializers.ModelSerializer):
target = serializers.PrimaryKeyRelatedField(
source='target.target', read_only=True, allow_null=True, default=None
)
class Meta:
model = NestedForeignKeySource
fields = ('target', )
model = NestedForeignKeySource.objects.create()
assert TestSerializer(model).data['target'] is None
def test_named_field_source(self):
class TestSerializer(serializers.ModelSerializer):