在django-restframework中将初始查询集限制为分页中的对象 [英] Restrict the initial queryset to objects in pagination in django-restframework

查看:98
本文介绍了在django-restframework中将初始查询集限制为分页中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django-rest-framework定义ModelViewSet.
我需要覆盖默认查询集,以在呈现响应之前对查询集对象执行一些处理.

I am defining a ModelViewSet using django-rest-framework.
I need to override the default queryset to perform some processing on the queryset objects before rendering the response.

此过程非常耗时,因此我只想对由于分页响应而对消费者实际可用的对象执行该过程,代替是将该过程应用于所有对象并应用分页之后完成我的处理,我可以注意到(如果我错了,请纠正我)是DRF中的默认行为.

This process is time-expensive so I would like to execute it only on the objects that will be actually available to the consumer due to the paginated response, instead of applying this process to ALL the objects and applying pagination AFTER finishing my processing, which I can notice (correct me if I'm wrong) is the default behavior in DRF.

简而言之,我需要的是:

In short what I need is:

如果默认查询集是1000 objects,但是分页仅限于25 objects per page,我想仅将那些25 objects应用于我的进程.请注意,除了分页之外,没有其他限制可以减少最终对象的数量.

If the default queryset is 1000 objects, but the pagination is restricted to 25 objects per page, I want to apply my process only those 25 objects. Please note there is no other constraints for reducing the final amount of objects other than pagination.

有没有办法做到这一点?
在这种情况下,覆盖默认查询集是个坏主意吗?

Is there a way to do this?
Is overriding the default queryset a bad idea in this case?

谢谢!

推荐答案

没有简单"的方法可以做到这一点.在Django REST框架中,分页是通过与渲染相同的方法完成的.

There is no "easy" way to do that. In Django REST framework pagination is done in the same method as rendering.

所以我想最好的方法是定义自己的Viewset并重新声明list方法:

So I guess the best way to go is to define your own Viewset and redeclare the list method:

from rest_framework.viewssets import ModelViewSet

class MyModelViewSet(ModelViewSet):

  def list(self, request, *args, **kwargs):
    self.object_list = self.filter_queryset(self.get_queryset())                
    if not self.allow_empty and not self.object_list:                           
      warnings.warn(                                                          
        'The `allow_empty` parameter is due to be deprecated. '             
        'To use `allow_empty=False` style behavior, You should override '   
        '`get_queryset()` and explicitly raise a 404 on empty querysets.',  
        PendingDeprecationWarning
      )              
      class_name = self.__class__.__name__                                    
      error_msg = self.empty_error % {'class_name': class_name}
      raise Http404(error_msg)                                                

    page = self.paginate_queryset(self.object_list)                             


    ## INSERT YOUR CODE HERE


    if page is not None:
      serializer = self.get_pagination_serializer(page)                       
    else:       
      serializer = self.get_serializer(self.object_list, many=True)           

    return Response(serializer.data)     

这篇关于在django-restframework中将初始查询集限制为分页中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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