本文介绍了django-tastypie:无法访问bund.request在脱水(self,bundle)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有同样问题的人,但对他的解决方案与我无关:
请参阅

I found someone with the same problem, but the solation for him doesn't work with me:seeDjango-Tastypie: How Do You Access (Http)request object in the Bundle?

我试图应用建议的解决方法:

I Am trying to apply the workaround suggested in:Django tastypie: Resource show different in detailed request as in list request

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

resulting in this code in my resources file (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对象。

You're probably running Tastypie 9.10 if you installed from pypi. The Bundle object in the pypi version does indeed not have a request object.

如果您升级到django-tastypie主机的git版本,请使用该代码,而不是解决问题。

If you upgrade to the git version of django-tastypie master and use that instead the problem should be resolved.

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

这篇关于django-tastypie:无法访问bund.request在脱水(self,bundle)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:11