Django-Tastypie:你如何在Bundle中访问(Http)请求对象?

时间:2011-09-12 14:41:41

标签: django api resources tastypie

我需要在Resource的脱水中访问HttpRequest对象 方法。

在文档中,它表明bundle.request是一个有效的属性(它是 在resources.html页面中)。当我尝试将其添加到我的代码中时,我得到了一个 声称Bundle'对象没有属性'request'的错误。 是什么给了什么?

2 个答案:

答案 0 :(得分:3)

Bundle对象有request属性。

class Bundle(object):
    """
    A small container for instances and converted data for the
    ``dehydrate/hydrate`` cycle.

    Necessary because the ``dehydrate/hydrate`` cycle needs to access data at
    different points.
    """
    def __init__(self, obj=None, data=None, request=None):
        self.obj = obj
        self.data = data or {}
        self.request = request or HttpRequest()

无论如何,您可以覆盖调用堆栈中高于dehydrate的Resource方法。

你能展示代码吗?

答案 1 :(得分:2)

我遇到了同样的问题,但在这里找到了正确答案: http://groups.google.com/group/django-tastypie/tree/browse_frm/thread/801f44af3f2dbe7b/a36f303380eacf96

似乎django-tasty-pie版本0.9.9没有这个属性,但版本0.9.10呢!

所以如果你使用buildout,请查看buildout.cfg 在版本下: 搜索django-tastypie = 0.9.9

删除此内容并查看您的安装选择或替换它:

django-tastypie = 0.9.10

我仍有这个问题,所以打开了一个新链接,请参阅:

django-tastypie: Cannot access bundle.request in dehydrate(self,bundle)

在上面的问题我发现,使用0.9.10是不够的,版本1.0.0测试版应该做tric ..