diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 67c3569fd..9e4fff449 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -13,7 +13,6 @@ response content is handled by parsers and renderers. from __future__ import unicode_literals from django.db import models from django.db.models.fields import FieldDoesNotExist, Field as DjangoModelField -from django.db.models import query from django.utils.functional import cached_property from django.utils.translation import ugettext_lazy as _ from rest_framework.compat import ( @@ -579,7 +578,8 @@ class ListSerializer(BaseSerializer): """ # Dealing with nested relationships, data can be a Manager, # so, first get a queryset from the Manager if needed - iterable = data.all() if isinstance(data, (models.Manager, query.QuerySet)) else data + iterable = data.all() if isinstance(data, models.Manager) else data + return [ self.child.to_representation(item) for item in iterable ] diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index a1d831905..35fa0e252 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -721,3 +721,28 @@ class TestSerializerMetaClass(TestCase): str(exception), "Cannot set both 'fields' and 'exclude' options on serializer ExampleSerializer." ) + + +class Issue2704TestCase(TestCase): + def test_queryset_all(self): + class TestSerializer(serializers.ModelSerializer): + additional_attr = serializers.CharField() + + class Meta: + model = OneFieldModel + fields = ('char_field', 'additional_attr') + + OneFieldModel.objects.create(char_field='abc') + qs = OneFieldModel.objects.all() + + for o in qs: + o.additional_attr = '123' + + serializer = TestSerializer(instance=qs, many=True) + + expected = [{ + 'char_field': 'abc', + 'additional_attr': '123', + }] + + assert serializer.data == expected