【问题标题】:Limit queryset in Django Tastypie在 Django Tastypie 中限制查询集
【发布时间】:2013-02-24 17:59:56
【问题描述】:

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

所以我正在尝试修改 sweetpie 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'

【问题讨论】:

    标签: django api rest tastypie


    【解决方案1】:

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

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

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

    【讨论】:

    • tastepie 中的默认分页在资源的元类中没有定义 paginator_class 的情况下有效。如果要实现自己的分页器,则必须将其定义到 Meta 类。
    • 没看到。谢谢,我把它从我的答案中删除了,因为它没用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 2012-03-31
    • 1970-01-01
    • 2018-10-29
    • 2016-04-07
    • 2012-07-11
    • 2017-10-13
    相关资源
    最近更新 更多