Disable pagination in Django tastypie?
Asked Answered
K

2

9

I have a tastypie api that I'm working on and in the list views for my api resources I'd like to get the entire list of data without pagination applied, regardless of the number of objects in the list. I don't need a custom paginator with a high limit, I'd like to disable pagination entirely.

I could potentially modify my client to deal with the pagination (the api is being accessed from a C++ DLL rather than a web browser so it's a little more complicated but possible) but if I can disable it that would be easier.

Is there a switch to disable the paginator for different resources, or possibly an api wide switch to disable pagination on all resources registered to that api object?

Kinsler answered 3/4, 2013 at 18:43 Comment(2)
github.com/toastdriven/django-tastypie/pull/639Preside
Could you clarify what that pull request is saying about my question? There seems to be a discussion about pagination limits in that thread but reading it over I'm not clear on what I should be doing to disable pagination. Should I set settings.API_LIMIT_PER_PAGE to None, should I set settings.API_MAX_LIMIT_PER_PAGE to None, is there something I can write into a resource's Meta class that will disable the limit etc..? The initial pull request is confusingly written for someone not familiar with the underlying mechanims of tastypie and the follow up comments don't really clarify much.Kinsler
K
11

To do this you need to set at least two different things.

In the site settings file, set

API_LIMIT_PER_PAGE = 0

In the resource Meta class that you want to disable pagination for, set:

class MyResource(ModelResource):
    ...
    class Meta:
        ...
        max_limit = None

Then if you navigate to the list view of the resource, the returned content should show a limit of 0.

Kinsler answered 10/4, 2013 at 14:24 Comment(0)
A
3

Alternative solution would be to define DummyPaginator class (originally described here: https://github.com/toastdriven/django-tastypie/issues/777), which should be part of tastypie (but it's not unfortunately...):

class DummyPaginator(object): 
    def __init__(self, request_data, objects, resource_uri=None,
                 limit=None, offset=0, max_limit=1000,
                 collection_name='objects'): 
        self.objects = objects
        self.collection_name = collection_name 

    def page(self):
        return { self.collection_name: self.objects, }

Then, in your resource Meta, you set:

 paginator_class = DummyPaginator
Askwith answered 3/6, 2013 at 20:39 Comment(1)
Hey, the page function shouldn't be nested within the init functionCartload

© 2022 - 2024 — McMap. All rights reserved.