【问题标题】:Django-Tastypie: How Do You Access (Http)request object in the Bundle?Django-Tastypie:如何访问 Bundle 中的 (Http) 请求对象?
【发布时间】:2011-11-15 10:05:31
【问题描述】:

我需要访问我的资源脱水中的 HttpRequest 对象 方法。

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

【问题讨论】:

    标签: django api resources tastypie


    【解决方案1】:

    Bundle 对象具有请求属性。

    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 方法。

    你能显示代码吗?

    【讨论】:

      【解决方案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 beta 应该可以做到这一点..

      【讨论】:

        猜你喜欢
        • 2012-01-28
        • 1970-01-01
        • 2016-11-08
        • 2020-11-04
        • 1970-01-01
        • 1970-01-01
        • 2012-11-16
        • 1970-01-01
        • 2017-05-28
        相关资源
        最近更新 更多