diff --git a/rest_framework/relations.py b/rest_framework/relations.py index ae4086ecf..6b693e4b2 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -101,17 +101,7 @@ class RelatedField(Field): def get_iterable(self, instance, source_attrs): relationship = get_attribute(instance, source_attrs) - relationship = relationship.all() if (hasattr(relationship, 'all')) else relationship - - if self.use_pk_only_optimization(): - # Optimized case, return mock objects only containing the pk attribute. - return [ - PKOnlyObject(pk=pk) - for pk in relationship.values_list('pk', flat=True) - ] - - # Standard case, return the object instances. - return relationship + return relationship.all() if (hasattr(relationship, 'all')) else relationship @property def choices(self): diff --git a/tests/test_relations_hyperlink.py b/tests/test_relations_hyperlink.py index e741e99be..f1b882edf 100644 --- a/tests/test_relations_hyperlink.py +++ b/tests/test_relations_hyperlink.py @@ -92,6 +92,12 @@ class HyperlinkedManyToManyTests(TestCase): with self.assertNumQueries(4): self.assertEqual(serializer.data, expected) + def test_many_to_many_retrieve_prefetch_related(self): + queryset = ManyToManySource.objects.all().prefetch_related('targets') + serializer = ManyToManySourceSerializer(queryset, many=True, context={'request': request}) + with self.assertNumQueries(2): + serializer.data + def test_reverse_many_to_many_retrieve(self): queryset = ManyToManyTarget.objects.all() serializer = ManyToManyTargetSerializer(queryset, many=True, context={'request': request}) diff --git a/tests/test_relations_pk.py b/tests/test_relations_pk.py index e95a877e1..f872a8dc5 100644 --- a/tests/test_relations_pk.py +++ b/tests/test_relations_pk.py @@ -71,6 +71,12 @@ class PKManyToManyTests(TestCase): with self.assertNumQueries(4): self.assertEqual(serializer.data, expected) + def test_many_to_many_retrieve_prefetch_related(self): + queryset = ManyToManySource.objects.all().prefetch_related('targets') + serializer = ManyToManySourceSerializer(queryset, many=True) + with self.assertNumQueries(2): + serializer.data + def test_reverse_many_to_many_retrieve(self): queryset = ManyToManyTarget.objects.all() serializer = ManyToManyTargetSerializer(queryset, many=True) @@ -188,6 +194,12 @@ class PKForeignKeyTests(TestCase): with self.assertNumQueries(3): self.assertEqual(serializer.data, expected) + def test_reverse_foreign_key_retrieve_prefetch_related(self): + queryset = ForeignKeyTarget.objects.all().prefetch_related('sources') + serializer = ForeignKeyTargetSerializer(queryset, many=True) + with self.assertNumQueries(2): + serializer.data + def test_foreign_key_update(self): data = {'id': 1, 'name': 'source-1', 'target': 2} instance = ForeignKeySource.objects.get(pk=1) diff --git a/tests/test_relations_slug.py b/tests/test_relations_slug.py index 7bac90460..cd2cb1ed6 100644 --- a/tests/test_relations_slug.py +++ b/tests/test_relations_slug.py @@ -54,7 +54,14 @@ class SlugForeignKeyTests(TestCase): {'id': 2, 'name': 'source-2', 'target': 'target-1'}, {'id': 3, 'name': 'source-3', 'target': 'target-1'} ] - self.assertEqual(serializer.data, expected) + with self.assertNumQueries(4): + self.assertEqual(serializer.data, expected) + + def test_foreign_key_retrieve_select_related(self): + queryset = ForeignKeySource.objects.all().select_related('target') + serializer = ForeignKeySourceSerializer(queryset, many=True) + with self.assertNumQueries(1): + serializer.data def test_reverse_foreign_key_retrieve(self): queryset = ForeignKeyTarget.objects.all() @@ -65,6 +72,12 @@ class SlugForeignKeyTests(TestCase): ] self.assertEqual(serializer.data, expected) + def test_reverse_foreign_key_retrieve_prefetch_related(self): + queryset = ForeignKeyTarget.objects.all().prefetch_related('sources') + serializer = ForeignKeyTargetSerializer(queryset, many=True) + with self.assertNumQueries(2): + serializer.data + def test_foreign_key_update(self): data = {'id': 1, 'name': 'source-1', 'target': 'target-2'} instance = ForeignKeySource.objects.get(pk=1)