diff options
| author | Tom Christie | 2013-01-26 21:37:43 +0000 |
|---|---|---|
| committer | Tom Christie | 2013-01-26 21:37:43 +0000 |
| commit | b5d8f50f9dcace3ad3c708ed518f23ff260f6bea (patch) | |
| tree | 7a0fb931ad5841b863359719075dd55f8370b3a1 /rest_framework/pagination.py | |
| parent | 4eb5861f3676781493af29f8e9fd87ec22e591aa (diff) | |
| parent | a75db4cfb8ed756c451bfda7ea0c73a73859216f (diff) | |
| download | django-rest-framework-b5d8f50f9dcace3ad3c708ed518f23ff260f6bea.tar.bz2 | |
Merge branch 'master' into many-fields
Diffstat (limited to 'rest_framework/pagination.py')
| -rw-r--r-- | rest_framework/pagination.py | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index d241ade7..92d41e0e 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -34,6 +34,17 @@ class PreviousPageField(serializers.Field): return replace_query_param(url, self.page_field, page) +class DefaultObjectSerializer(serializers.Field): + """ + If no object serializer is specified, then this serializer will be applied + as the default. + """ + + def __init__(self, source=None, context=None): + # Note: Swallow context kwarg - only required for eg. ModelSerializer. + super(DefaultObjectSerializer, self).__init__(source=source) + + class PaginationSerializerOptions(serializers.SerializerOptions): """ An object that stores the options that may be provided to a @@ -44,7 +55,7 @@ class PaginationSerializerOptions(serializers.SerializerOptions): def __init__(self, meta): super(PaginationSerializerOptions, self).__init__(meta) self.object_serializer_class = getattr(meta, 'object_serializer_class', - serializers.Field) + DefaultObjectSerializer) class BasePaginationSerializer(serializers.Serializer): @@ -62,14 +73,13 @@ class BasePaginationSerializer(serializers.Serializer): super(BasePaginationSerializer, self).__init__(*args, **kwargs) results_field = self.results_field object_serializer = self.opts.object_serializer_class - self.fields[results_field] = object_serializer(source='object_list') - def to_native(self, obj): - """ - Prevent default behaviour of iterating over elements, and serializing - each in turn. - """ - return self.convert_object(obj) + if 'context' in kwargs: + context_kwarg = {'context': kwargs['context']} + else: + context_kwarg = {} + + self.fields[results_field] = object_serializer(source='object_list', **context_kwarg) class PaginationSerializer(BasePaginationSerializer): |
