mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
Add tests for nested FKs
This commit is contained in:
parent
1f6af163fe
commit
250a7231d3
|
@ -15,12 +15,18 @@ class ForeignKeySource(models.Model):
|
||||||
|
|
||||||
|
|
||||||
class ForeignKeySourceSerializer(serializers.ModelSerializer):
|
class ForeignKeySourceSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
depth = 1
|
||||||
|
model = ForeignKeySource
|
||||||
|
|
||||||
|
|
||||||
|
class FlatForeignKeySourceSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ForeignKeySource
|
model = ForeignKeySource
|
||||||
|
|
||||||
|
|
||||||
class ForeignKeyTargetSerializer(serializers.ModelSerializer):
|
class ForeignKeyTargetSerializer(serializers.ModelSerializer):
|
||||||
sources = ForeignKeySourceSerializer()
|
sources = FlatForeignKeySourceSerializer()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ForeignKeyTarget
|
model = ForeignKeyTarget
|
||||||
|
@ -36,6 +42,16 @@ class ReverseForeignKeyTests(TestCase):
|
||||||
source = ForeignKeySource(name='source-%d' % idx, target=target)
|
source = ForeignKeySource(name='source-%d' % idx, target=target)
|
||||||
source.save()
|
source.save()
|
||||||
|
|
||||||
|
def test_foreign_key_retrieve(self):
|
||||||
|
queryset = ForeignKeySource.objects.all()
|
||||||
|
serializer = ForeignKeySourceSerializer(queryset)
|
||||||
|
expected = [
|
||||||
|
{'id': 1, 'name': u'source-1', 'target': {'id': 1, 'name': u'target-1'}},
|
||||||
|
{'id': 2, 'name': u'source-2', 'target': {'id': 1, 'name': u'target-1'}},
|
||||||
|
{'id': 3, 'name': u'source-3', 'target': {'id': 1, 'name': u'target-1'}},
|
||||||
|
]
|
||||||
|
self.assertEquals(serializer.data, expected)
|
||||||
|
|
||||||
def test_reverse_foreign_key_retrieve(self):
|
def test_reverse_foreign_key_retrieve(self):
|
||||||
queryset = ForeignKeyTarget.objects.all()
|
queryset = ForeignKeyTarget.objects.all()
|
||||||
serializer = ForeignKeyTargetSerializer(queryset)
|
serializer = ForeignKeyTargetSerializer(queryset)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user