I am working on an api built with Django Rest Framework
. I have defined several model
classes and I have also created some filters to apply on certain queries that happen in the specified api-endpoints
.
我正在使用Django Rest Framework构建的api。我已经定义了几个模型类,并且我还创建了一些过滤器来应用于在指定的api-endpoints中发生的某些查询。
I am trying to apply LIMIT in the queryset
but I would prefer to not use the Django notation e.x. Entry.objects.all()[:5]
. Instead I would like to be able to apply the limit from inside the filter associated with the model.
我试图在查询集中应用LIMIT但我宁愿不使用Django表示法e.x. Entry.objects.all()[:5]。相反,我希望能够从与模型关联的过滤器内部应用限制。
Until now I haven't find any solution. I am thinking that I should find a way to define a filter with default value something that would result in not limiting the queryset and if a request reaches the endpoint and contains something like ?filter=10
then the queryset should be limited to the first 10.
直到现在我还没有找到任何解决方案。我想我应该找到一种方法来定义一个默认值的过滤器,这将导致不限制查询集,如果请求到达端点并包含类似?filter = 10,那么查询集应该限制在前10个。
3 个解决方案
#1
3
You can use Django Rest Framework pagination. The pagination_class LimitOffsetPagination
give you the ability to limit the number of returned entries in a query_param.
您可以使用Django Rest Framework分页。 pagination_class LimitOffsetPagination使您能够限制query_param中返回的条目数。
http://www.django-rest-framework.org/api-guide/pagination/
http://www.django-rest-framework.org/api-guide/pagination/
#2
1
you should use the pagination api from django-rest
你应该使用django-rest的分页api
http://www.django-rest-framework.org/api-guide/pagination/
http://www.django-rest-framework.org/api-guide/pagination/
look at the limit
option
看一下限制选项
#3
1
You can extend or customize pagination classes available in drf
您可以扩展或自定义drf中可用的分页类
class UserSpecificPagination(LimitOffsetPagination):
def get_limit(self, request):
if logic_met(request.user):
self.max_limit = custom_limit
return super(UserSpecificPagination, self).get_limit(request)
set the class as pagination_class
in listapiview
or DRF settings
在listapiview或DRF设置中将类设置为pagination_class
#1
3
You can use Django Rest Framework pagination. The pagination_class LimitOffsetPagination
give you the ability to limit the number of returned entries in a query_param.
您可以使用Django Rest Framework分页。 pagination_class LimitOffsetPagination使您能够限制query_param中返回的条目数。
http://www.django-rest-framework.org/api-guide/pagination/
http://www.django-rest-framework.org/api-guide/pagination/
#2
1
you should use the pagination api from django-rest
你应该使用django-rest的分页api
http://www.django-rest-framework.org/api-guide/pagination/
http://www.django-rest-framework.org/api-guide/pagination/
look at the limit
option
看一下限制选项
#3
1
You can extend or customize pagination classes available in drf
您可以扩展或自定义drf中可用的分页类
class UserSpecificPagination(LimitOffsetPagination):
def get_limit(self, request):
if logic_met(request.user):
self.max_limit = custom_limit
return super(UserSpecificPagination, self).get_limit(request)
set the class as pagination_class
in listapiview
or DRF settings
在listapiview或DRF设置中将类设置为pagination_class