mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
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:
commit
560f428e24
|
@ -43,8 +43,9 @@ class DefaultObjectSerializer(serializers.Field):
|
|||
as the default.
|
||||
"""
|
||||
|
||||
def __init__(self, source=None, context=None):
|
||||
# Note: Swallow context kwarg - only required for eg. ModelSerializer.
|
||||
def __init__(self, source=None, many=None, context=None):
|
||||
# Note: Swallow context and many kwargs - only required for
|
||||
# eg. ModelSerializer.
|
||||
super(DefaultObjectSerializer, self).__init__(source=source)
|
||||
|
||||
|
||||
|
@ -82,7 +83,9 @@ class BasePaginationSerializer(serializers.Serializer):
|
|||
else:
|
||||
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):
|
||||
|
|
|
@ -412,6 +412,15 @@ class CustomPaginationSerializer(pagination.BasePaginationSerializer):
|
|||
results_field = 'objects'
|
||||
|
||||
|
||||
class CustomFooSerializer(serializers.Serializer):
|
||||
foo = serializers.CharField()
|
||||
|
||||
|
||||
class CustomFooPaginationSerializer(pagination.PaginationSerializer):
|
||||
class Meta:
|
||||
object_serializer_class = CustomFooSerializer
|
||||
|
||||
|
||||
class TestCustomPaginationSerializer(TestCase):
|
||||
def setUp(self):
|
||||
objects = ['john', 'paul', 'george', 'ringo']
|
||||
|
@ -434,6 +443,16 @@ class TestCustomPaginationSerializer(TestCase):
|
|||
}
|
||||
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):
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user