Add test and fix for #2704

This commit is contained in:
José Padilla 2015-06-24 22:02:28 -04:00 committed by José Padilla
parent cd0c9b7555
commit d6e30c75ff
2 changed files with 27 additions and 2 deletions

View File

@ -13,7 +13,6 @@ response content is handled by parsers and renderers.
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import models from django.db import models
from django.db.models.fields import FieldDoesNotExist, Field as DjangoModelField 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.functional import cached_property
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from rest_framework.compat import ( from rest_framework.compat import (
@ -579,7 +578,8 @@ class ListSerializer(BaseSerializer):
""" """
# Dealing with nested relationships, data can be a Manager, # Dealing with nested relationships, data can be a Manager,
# so, first get a queryset from the Manager if needed # 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 [ return [
self.child.to_representation(item) for item in iterable self.child.to_representation(item) for item in iterable
] ]

View File

@ -721,3 +721,28 @@ class TestSerializerMetaClass(TestCase):
str(exception), str(exception),
"Cannot set both 'fields' and 'exclude' options on serializer ExampleSerializer." "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