简体   繁体   English

使用Django-Rest排序游标

[英]Ordering cursor pagination with Django-Rest

I'm building an API with Django-Rest-Framework and set the cursor pagination - by default ordered by 'created' filter, what work really fine to some views. 我正在使用Django-Rest-Framework构建API并设置光标分页-默认情况下按“创建的”过滤器排序,对于某些视图来说,确实可以正常工作。

But I have this one that I want order by other fields (last_update and visits). 但是我有这个我想按其他字段(last_update和访问次数)排序的命令。 I put the ordering field in my view, but doesn't work. 我将订购字段置于视图中,但不起作用。

class StationList(generics.ListAPIView):

    """
    List all stations.
    """
    ordering = ('-last_update', '-visits',)
    queryset = Station.objects.all()
    serializer_class = StationSerializer
    permission_classes = (permissions.IsAuthenticated,)

That is part of my setting.py 那是我的setting.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,
}

What I need to do to it works? 我需要做的工作吗?

Try listing your fields under ordering_fields as well, I think you still need to whitelist them for sorting: 尝试将您的字段也列出在ordering_fields下,我认为您仍然需要将它们列入白名单以进行排序:

ordering = ('-last_update', '-visits',)
ordering_fields= ('-last_update', '-visits',)

i think you should use a array instead a tuple. 我认为你应该使用数组而不是元组。 try: 尝试:

ordering = ['-last_update', '-visits']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM