0

我正在用 django 和 sweetpie 构建一个应用程序,并尝试通过 ajax 加载对象。例如,我显示 3 个对象,然后一旦用户到达最后一个对象,我再加载 3 个等。

所以我试图修改tastepie api中的查询集,但找不到如何做到这一点。在 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'
4

1 回答 1

1

找到了解决方案。这实际上很容易,但我会写下答案,以防它对其他人有用。

解决方案是使用分页器(我没有在谷歌上搜索正确的术语,所以我一开始没有找到)。我的ajax调用我发送offsetlimit参数。

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

于 2013-02-24T18:15:51.600 回答