mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Make 'results_field' attribute of BasePaginationSerializer public.
This commit is contained in:
parent
2284e592de
commit
ae8a827004
|
@ -104,7 +104,9 @@ For more complex requirements such as serialization that differs depending on th
|
||||||
|
|
||||||
To create a custom pagination serializer class you should override `pagination.BasePaginationSerializer` and set the fields that you want the serializer to return.
|
To create a custom pagination serializer class you should override `pagination.BasePaginationSerializer` and set the fields that you want the serializer to return.
|
||||||
|
|
||||||
For example, to nest a pair of links labelled 'prev' and 'next' you might use something like this.
|
You can also override the name used for the object list field, by setting the `results_field` attribute, which defaults to `'results'`.
|
||||||
|
|
||||||
|
For example, to nest a pair of links labelled 'prev' and 'next', and set the name for the results field to 'objects', you might use something like this.
|
||||||
|
|
||||||
class LinksSerializer(serializers.Serializer):
|
class LinksSerializer(serializers.Serializer):
|
||||||
next = pagination.NextURLField(source='*')
|
next = pagination.NextURLField(source='*')
|
||||||
|
@ -114,4 +116,6 @@ For example, to nest a pair of links labelled 'prev' and 'next' you might use so
|
||||||
links = LinksSerializer(source='*') # Takes the page object as the source
|
links = LinksSerializer(source='*') # Takes the page object as the source
|
||||||
total_results = serializers.Field(source='paginator.count')
|
total_results = serializers.Field(source='paginator.count')
|
||||||
|
|
||||||
|
results_field = 'objects'
|
||||||
|
|
||||||
[cite]: https://docs.djangoproject.com/en/dev/topics/pagination/
|
[cite]: https://docs.djangoproject.com/en/dev/topics/pagination/
|
||||||
|
|
|
@ -52,14 +52,14 @@ class BasePaginationSerializer(serializers.Serializer):
|
||||||
to make implementing custom serializers more easy.
|
to make implementing custom serializers more easy.
|
||||||
"""
|
"""
|
||||||
_options_class = PaginationSerializerOptions
|
_options_class = PaginationSerializerOptions
|
||||||
_results_field = 'results'
|
results_field = 'results'
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Override init to add in the object serializer field on-the-fly.
|
Override init to add in the object serializer field on-the-fly.
|
||||||
"""
|
"""
|
||||||
super(BasePaginationSerializer, self).__init__(*args, **kwargs)
|
super(BasePaginationSerializer, self).__init__(*args, **kwargs)
|
||||||
results_field = self._results_field
|
results_field = self.results_field
|
||||||
object_serializer = self.opts.object_serializer_class
|
object_serializer = self.opts.object_serializer_class
|
||||||
self.fields[results_field] = object_serializer(source='object_list')
|
self.fields[results_field] = object_serializer(source='object_list')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user