如何在Django REST框架ViewSet子类中使用分页? [英] How do you use pagination in a Django REST framework ViewSet subclass?

查看:138
本文介绍了如何在Django REST框架ViewSet子类中使用分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 viewsets.ViewSet 的简单子类,它看起来像:

I have a simple subclass of viewsets.ViewSet which looks like:

from rest_framework import viewsets
from rest_framework.response import Response

from ..models import Entry, Sitting, Source, Venue
from .serializers import (
    SittingSerializer, SittingWithEntriesSerializer,
)

class SittingViewSet(viewsets.ViewSet):

    def list(self, request, version=None):
        queryset = Sitting.objects.order_by('id')
        serializer = SittingSerializer(
            queryset, many=True, context={'request': request}
        )
        return Response(serializer.data)

    def retrieve(self, request, pk=None, version=None):
        prefetch_qs = Entry.objects.select_related('speaker')
        queryset = Sitting.objects.order_by('id') \
            .prefetch_related(Prefetch('entry_set', queryset=prefetch_qs))
        sitting = get_object_or_404(queryset, pk=pk)
        serializer = SittingWithEntriesSerializer(
            sitting, context={'request': request}
        )
        return Response(serializer.data)

但是,列表视图没有分页,因为如果使用<$ c的子类$ c> ModelViewSet 。我正在使用的设置是:

However, the list view isn't paginated, as it is if you use a subclass of ModelViewSet. The settings I'm using are:

# Django Rest Framework settings:
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': ('pombola.api.permissions.ReadOnly',),
    'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.URLPathVersioning',
    'PAGE_SIZE': 10,
}

文档建议查看 mixins.ListModelMixin generics.GenericAPIView 类,但我不容易看到如何重新应用它们将这些结果分页到这些ViewSet方法。

The documentation suggests looking at the source code for the mixins.ListModelMixin and generics.GenericAPIView classes, but I can't easily see how to reapply what they do to paginate results to these ViewSet methods.

有人可以建议最简单的方法是更改此示例以获取列表视图的分页吗?

Could anyone suggest what the simplest way would be to change this example to get pagination for the list view?

推荐答案

这是迟来的答案,我写了一个用于Django Rest Framework的Q& A样式示例,它使非通用视图集具有分页功能。

Although this comes late as an answer, I wrote a Q&A style example for Django Rest Framework which enables non generic viewsets to have pagination.

默认情况下,仅视图集。 GenericViewSet 具有自动分页功能(如果您在设置中启用分页功能,则为 ),因为它继承自 generics.GenericAPIView

By default, only the viewsets.GenericViewSet has automatic pagination (if you enable pagination in your settings of course), because it inherits from generics.GenericAPIView.

这为您提供2种选择:


  1. 简单的方法:

mixins.ListModelMixin 提供分页功能,因此您只需声明如下的视图集即可:

mixins.ListModelMixin provides pagination, so you can simply declare your viewset as follows:

class SittingViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):

现在有一个带有分页的 list()方法。

and you now have a list() method with pagination.

更困难的方法: *

您可以按照给出的示例进行操作上方,并在您的视图集中创建分页方法。

list()代码必须从 mixins.ListModelMixin 以上。

paginator() paginate_queryset() get_paginated_response()方法可以从文档或[ generics.GenericApiView ] [4]

You can follow the example given above and create a pagination method inside your viewset.
The list() code must be derived from the source code of the mixins.ListModelMixin provided above.
The paginator(), paginate_queryset() and get_paginated_response() methods can be copied from the Documentation or the source code of [generics.GenericApiView][4]







  • 我没有为第二个选项添加代码示例,因为它只是上述链接的复制/粘贴。
    我在这篇文章中做了一个相关的答案:在DRF APIView中工作

  • 这篇关于如何在Django REST框架ViewSet子类中使用分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆