mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-14 18:22:19 +03:00
parent
6c0c69ed65
commit
85c588b0c1
|
@ -174,7 +174,7 @@ class RelatedField(Field):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Standard case, return the object instance.
|
# 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):
|
def get_choices(self, cutoff=None):
|
||||||
queryset = self.get_queryset()
|
queryset = self.get_queryset()
|
||||||
|
|
|
@ -23,6 +23,8 @@ from django.utils import six
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from rest_framework.compat import postgres_fields, unicode_repr
|
from rest_framework.compat import postgres_fields, unicode_repr
|
||||||
|
|
||||||
|
from .models import NestedForeignKeySource
|
||||||
|
|
||||||
|
|
||||||
def dedent(blocktext):
|
def dedent(blocktext):
|
||||||
return '\n'.join([line[12:] for line in blocktext.splitlines()[1:-1]])
|
return '\n'.join([line[12:] for line in blocktext.splitlines()[1:-1]])
|
||||||
|
@ -1164,6 +1166,25 @@ class Test5004UniqueChoiceField(TestCase):
|
||||||
|
|
||||||
|
|
||||||
class TestFieldSource(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):
|
def test_named_field_source(self):
|
||||||
class TestSerializer(serializers.ModelSerializer):
|
class TestSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user