django- tastype:不能访问bundle.请求脱水(自我,捆绑)



我找到了一个有同样问题的人,但是对他的隔离对我不起作用:看到Django-Tastypie:如何访问Bundle中的Http请求对象?

我正试图应用建议的解决方案:Django tastypie:资源显示在详细请求中不同于列表请求

导致我的资源文件(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对象确实没有request对象。

如果你升级到git版本的django-tastypie master并使用它,问题就会解决。

pip uninstall django-tastypie 
pip install -e git+https://github.com/toastdriven/django-tastypie.git#egg=django-tastypie

最新更新