How to limit query results to Django Rest filters

I am working on an api built using the Django Rest Framework . I have defined several model classes, and I have also created some filters to apply to specific requests that occur in the specified api-endpoints .

I am trying to apply LIMIT to queryset , but I would prefer not to use the Django ex Entry.objects.all()[:5] notation. Instead, I would like to use the limit from the filter associated with the model.

So far I have not found any solution. I think I should find a way to define a filter with a default value, which will result in it not limiting the set of requests, and if the request reaches the endpoint and contains something like ?filter=10 , then the set of requests should be limited first 10.

+7
python django django-rest-framework django-filter
source share
3 answers

You can use the Django Rest Framework pagination. The pagination_class LimitOffsetPagination function gives you the ability to limit the number of returned records in query_param.

http://www.django-rest-framework.org/api-guide/pagination/

+3
source share

you should use pagination api from django-rest

http://www.django-rest-framework.org/api-guide/pagination/

view the limit option

+1
source share

You can extend or customize the pagination classes available in 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

+1
source share

All Articles