mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
Define 'page' query field name in one place.
This commit is contained in:
parent
6fbd411254
commit
5454162b04
|
@ -3,7 +3,11 @@ from rest_framework import serializers
|
||||||
# TODO: Support URLconf kwarg-style paging
|
# TODO: Support URLconf kwarg-style paging
|
||||||
|
|
||||||
|
|
||||||
class NextPageField(serializers.Field):
|
class PageField(serializers.Field):
|
||||||
|
page_field = 'page'
|
||||||
|
|
||||||
|
|
||||||
|
class NextPageField(PageField):
|
||||||
"""
|
"""
|
||||||
Field that returns a link to the next page in paginated results.
|
Field that returns a link to the next page in paginated results.
|
||||||
"""
|
"""
|
||||||
|
@ -12,16 +16,16 @@ class NextPageField(serializers.Field):
|
||||||
return None
|
return None
|
||||||
page = value.next_page_number()
|
page = value.next_page_number()
|
||||||
request = self.context.get('request')
|
request = self.context.get('request')
|
||||||
relative_url = '?page=%d' % page
|
relative_url = '?%s=%d' % (self.page_field, page)
|
||||||
if request:
|
if request:
|
||||||
for field, value in request.QUERY_PARAMS.iteritems():
|
for field, value in request.QUERY_PARAMS.iteritems():
|
||||||
if field != 'page':
|
if field != self.page_field:
|
||||||
relative_url += '&%s=%s' % (field, value)
|
relative_url += '&%s=%s' % (field, value)
|
||||||
return request.build_absolute_uri(relative_url)
|
return request.build_absolute_uri(relative_url)
|
||||||
return relative_url
|
return relative_url
|
||||||
|
|
||||||
|
|
||||||
class PreviousPageField(serializers.Field):
|
class PreviousPageField(PageField):
|
||||||
"""
|
"""
|
||||||
Field that returns a link to the previous page in paginated results.
|
Field that returns a link to the previous page in paginated results.
|
||||||
"""
|
"""
|
||||||
|
@ -30,10 +34,10 @@ class PreviousPageField(serializers.Field):
|
||||||
return None
|
return None
|
||||||
page = value.previous_page_number()
|
page = value.previous_page_number()
|
||||||
request = self.context.get('request')
|
request = self.context.get('request')
|
||||||
relative_url = '?page=%d' % page
|
relative_url = '?%s=%d' % (self.page_field, page)
|
||||||
if request:
|
if request:
|
||||||
for field, value in request.QUERY_PARAMS.iteritems():
|
for field, value in request.QUERY_PARAMS.iteritems():
|
||||||
if field != 'page':
|
if field != self.page_field:
|
||||||
relative_url += '&%s=%s' % (field, value)
|
relative_url += '&%s=%s' % (field, value)
|
||||||
return request.build_absolute_uri(relative_url)
|
return request.build_absolute_uri(relative_url)
|
||||||
return relative_url
|
return relative_url
|
||||||
|
|
Loading…
Reference in New Issue
Block a user