【发布时间】:2016-04-27 12:54:29
【问题描述】:
我正在使用 Django-Rest-Framework 构建一个 API 并设置光标分页 - 默认情况下按“已创建”过滤器排序,这对某些视图非常有效。
但我有这个我想按其他字段(last_update 和访问)排序。我将排序字段放在我的视图中,但不起作用。
class StationList(generics.ListAPIView):
"""
List all stations.
"""
ordering = ('-last_update', '-visits',)
queryset = Station.objects.all()
serializer_class = StationSerializer
permission_classes = (permissions.IsAuthenticated,)
这是我设置的一部分。py
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
),
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.CursorPagination',
'PAGE_SIZE': 12,
}
我需要做什么才能让它起作用?
【问题讨论】:
-
请记住,您可以用于
CursorPagination的订购字段有一些限制:django-rest-framework.org/api-guide/pagination/…
标签: python django pagination django-rest-framework