2013-02-24 56 views
0

我正在用django和tastypie構建應用程序,並嘗試通過ajax加載對象。 例如我顯示3個對象,然後一旦用戶到達最後我加載3更多等在Django中限制查詢集Tastypie

所以我想修改tastypie api中的queryset,但無法找到如何做到這一點。 在ajax調用中,我發送一個參數start,它定義了限制的開始。但是我不知道我可以在哪裏訪問這個參數並修改查詢集。

class EntryResource(ModelResource): 

    def dehydrate(self, bundle): 
     # I can get the parameter here but it's not useful 
     start = bundle.request['start'] 
     return bundle 

    def get_object_list(self, request): 
     # I can modify the objects returned here but how can I access bundle.request ? 
     return super(EntryResource, self).get_object_list(request).filter(active=True) 

    class Meta: 
     queryset = Entry.objects.all() 
     resource_name = 'entry' 

回答

1

找到解決方案。這實際上很簡單,但我會寫出答案,以防其他人有用。

解決方案是使用paginator(我沒有用google搜索正確的術語,所以我一開始沒有找到它)。 我在ajax調用中發送offsetlimit參數。

更多:tastypie作品http://django-tastypie.readthedocs.org/en/latest/paginator.html

+1

默認分頁沒有定義在元類資源的paginator_class。如果你想實現你自己的分頁器,那麼你必須將它定義到Meta類。 – UnLiMiTeD 2013-02-25 14:51:46

+0

沒有看到。謝謝,我從答案中刪除了它,因爲它沒用 – romainberger 2013-02-25 16:17:01