django-tastypie:无法访问脱水中的bundle.request(self,bundle)

2024-05-12

我发现有人有同样的问题,但他的安慰对我不起作用: 看Django-Tastypie:如何访问 Bundle 中的 (Http)request 对象? https://stackoverflow.com/questions/7389632

我正在尝试应用以下建议的解决方法:Django tastypie:资源在详细请求中显示与列表请求中不同 https://stackoverflow.com/questions/8242969/

在我的资源文件(api.py)中生成此代码:

class LocationResource(ModelResource):
    locationtype = fields.ForeignKey(LocationTypeResource, 'locationtype', full=False)

    class Meta:
        queryset = Location.objects.all()
        resource_name = 'location'
        excludes = ['public_anonymous', 'public_authorized','x','y','z']
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()

    def dehydrate(self, bundle):
        if bundle.request:
            if bundle.request.path: == "/api/v1/location/":
                del bundle.data['description']
            else:

                logger.debug("request availabe")
        else:
            logger.debug("request not availabe")
        return bundle

我收到错误:

{
error_message: "'Bundle' object has no attribute 'request'",
traceback: "Traceback (most recent call last):

  File "/home/michel/django/sites/regelwater/eggs/django_tastypie-0.9.10-py2.6.egg/tastypie/resources.py", line 178, in wrapper
    response = callback(request, *args, **kwargs)

  File "/home/michel/django/sites/regelwater/eggs/django_tastypie-0.9.10-py2.6.egg/tastypie/resources.py", line 379, in dispatch_list
    return self.dispatch('list', request, **kwargs)

  File "/home/michel/django/sites/regelwater/eggs/django_tastypie-0.9.10-py2.6.egg/tastypie/resources.py", line 409, in dispatch
    response = method(request, **kwargs)

  File "/home/michel/django/sites/regelwater/eggs/django_tastypie-0.9.10-py2.6.egg/tastypie/resources.py", line 987, in get_list
    to_be_serialized['objects'] = [self.full_dehydrate(obj=obj) for obj in to_be_serialized['objects']]

  File "/home/michel/django/sites/regelwater/eggs/django_tastypie-0.9.10-py2.6.egg/tastypie/resources.py", line 638, in full_dehydrate
    bundle = self.dehydrate(bundle)

  File "/home/michel/django/sites/regelwater/reservoir/api.py", line 71, in dehydrate
    if bundle.request:

AttributeError: 'Bundle' object has no attribute 'request'
"
}

如果您从 pypi 安装,您可能正在运行 Tastypie 9.10。 pypi版本中的Bundle对象确实没有请求对象 https://github.com/toastdriven/django-tastypie/blob/a9b6b23d6c3cc62ed3379089fa2d57d2d5b341ef/tastypie/bundle.py#L10.

如果您升级到 django-tastypie master 的 git 版本并使用它,问题应该可以解决。

pip uninstall django-tastypie 
pip install -e git+https://github.com/toastdriven/django-tastypie.git#egg=django-tastypie
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

django-tastypie:无法访问脱水中的bundle.request(self,bundle) 的相关文章

随机推荐