Merge pull request #1826 from swistakm/fix/explicit-many-in-pagination-serializer-results

Use explicit `many=True` in PaginationSerializer on `object_serializer` instantiation
This commit is contained in:
Tom Christie 2014-09-03 16:33:42 +01:00
commit 560f428e24
2 changed files with 25 additions and 3 deletions

View File

@ -43,8 +43,9 @@ class DefaultObjectSerializer(serializers.Field):
as the default. as the default.
""" """
def __init__(self, source=None, context=None): def __init__(self, source=None, many=None, context=None):
# Note: Swallow context kwarg - only required for eg. ModelSerializer. # Note: Swallow context and many kwargs - only required for
# eg. ModelSerializer.
super(DefaultObjectSerializer, self).__init__(source=source) super(DefaultObjectSerializer, self).__init__(source=source)
@ -82,7 +83,9 @@ class BasePaginationSerializer(serializers.Serializer):
else: else:
context_kwarg = {} context_kwarg = {}
self.fields[results_field] = object_serializer(source='object_list', **context_kwarg) self.fields[results_field] = object_serializer(source='object_list',
many=True,
**context_kwarg)
class PaginationSerializer(BasePaginationSerializer): class PaginationSerializer(BasePaginationSerializer):

View File

@ -412,6 +412,15 @@ class CustomPaginationSerializer(pagination.BasePaginationSerializer):
results_field = 'objects' results_field = 'objects'
class CustomFooSerializer(serializers.Serializer):
foo = serializers.CharField()
class CustomFooPaginationSerializer(pagination.PaginationSerializer):
class Meta:
object_serializer_class = CustomFooSerializer
class TestCustomPaginationSerializer(TestCase): class TestCustomPaginationSerializer(TestCase):
def setUp(self): def setUp(self):
objects = ['john', 'paul', 'george', 'ringo'] objects = ['john', 'paul', 'george', 'ringo']
@ -434,6 +443,16 @@ class TestCustomPaginationSerializer(TestCase):
} }
self.assertEqual(serializer.data, expected) self.assertEqual(serializer.data, expected)
def test_custom_pagination_serializer_with_custom_object_serializer(self):
objects = [
{'foo': 'bar'},
{'foo': 'spam'}
]
paginator = Paginator(objects, 1)
page = paginator.page(1)
serializer = CustomFooPaginationSerializer(page)
serializer.data
class NonIntegerPage(object): class NonIntegerPage(object):