1👍
I actually did go with a custom pagination class:
class Unpaginatable(PageNumberPagination):
def paginate_queryset(self, queryset, request, view=None):
if getattr(request, 'get_all', False):
return None
return super(BuildListPagination, self).paginate_queryset(queryset, request, view=view)
Now I just have to set request.get_all = True
in my viewset and I get all the items.
3👍
I used a similer appraoch to accepted answer
class Unpaginatable(PageNumberPagination):
def paginate_queryset(self, queryset, request, view=None):
if request.query_params.get('get_all', False) == 'true':
return None
return super(BuildListPagination, self).paginate_queryset(queryset, request, view=view)
now if you pass ?get_all=true
while making the request, you will get unpaginated response.
- [Django]-Allow only one concurrent login per user in django app
- [Django]-How to store HDF5 (HDF Store) in a Django model field
- [Django]-Django urlpatterns frustrating problem with trailing slashes
- [Django]-Find out Python version from source code (or Heroku)
3👍
If you are using page number pagination style, the below solution may be better.
def paginate_queryset(self, queryset, request, view=None):
if 'page' not in request.query_params:
return None
return super().paginate_queryset(queryset, request, view)
So you simply send a request without the page query_param.
1👍
thnaks to this answer,
Else, if you use limit/offset you can use this in Generic List API View class:
def paginate_queryset(self, queryset):
if 'limit' not in self.request.query_params:
return None
return super().paginate_queryset(queryset)
It worked with python 3.9/ Django 4.0, In my case, this method had no argument named request and view, so I fixed it.
this will also won’t render paginator json, when the response is not paginated.
- [Django]-Django Heroku not serving static files when Debug=False
- [Django]-Docker replicas vs gunicorn workers in production
- [Django]-Render to response to a redirected url in Django
- [Django]-How should error corresponding to an AJAX request be passed to and handled at the client-side?
- [Django]-TemplateDoesNotExist in project folder django 1.8
0👍
You can approximate this with a request limit set to an unfeasibly large number, but you need to configure Django first:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination'
}
Then make a ridiculously high limit request:
GET https://api.example.org/accounts/?limit=999999999999
- [Django]-Django Row Number in Pagination
- [Django]-Django-allauth returns error "Reverse … with arguments '()' and keyword arguments '{}' not found"
0👍
Actually DRF docs for this is very unclear, and there is no clear answer address this issue. After reading the source code, the followling code works for me.
from rest_framework.pagination import LimitOffsetPagination
class Unpaginated(LimitOffsetPagination):
def paginate_queryset(self, queryset, request, view=None):
self.count = self.get_count(queryset)
self.limit = self.get_limit(request)
self.offset = self.get_offset(request)
self.request = request
self.display_page_controls = False
return list(queryset)
class SomeViewSet(viewsets.ModelViewSet):
queryset = SomeModel.objects.all().order_by("-id")
pagination_class = Unpaginated
The key here is to override the paginate_queryset
function in base class and return the whole queryset.
However, overriding this function is not documented at all in the docs or I just missed it.
- [Django]-Creating a Django admin account: null value of password
- [Django]-Django instance.id=None when uploading image
- [Django]-Filtering queryset if one value is greater than another value
- [Django]-How to define the admin import format for a field using django-import-export
- [Django]-How can I disable/remove authorize button in swagger drf_yasg (maintain CSRF) – django
0👍
I solve this problem in the following way:
- make your own pagination class and inherit it from PageNumberPagination
- redefine or add your code (with super()) in get_page_size function.
For me works this case:
def get_page_size(self, request):
super().get_page_size(request)
…….
self.page_size = self.max_page_size
return self.page_size
- [Django]-Django celery only calls 1 of 2 apply_async task
- [Django]-Celery: running a worker with superuser privileges