Skip to content

Commit

Permalink
Merge pull request #3076 from jpadilla/issues/2704
Browse files Browse the repository at this point in the history
Progressing #2704
  • Loading branch information
tomchristie committed Jun 25, 2015
2 parents cd0c9b7 + d6e30c7 commit df7c114
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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 (
Expand Down Expand Up @@ -579,7 +578,8 @@ def to_representation(self, data):
"""
# 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
]
Expand Down
25 changes: 25 additions & 0 deletions tests/test_model_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,3 +721,28 @@ class Meta:
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

0 comments on commit df7c114

Please sign in to comment.