1

同じ問題を抱えている人を見つけましたが、彼の解決策はうまくいきません 。Django-Tastypie:バンドル内の(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'
"
}
4

1 に答える 1

3

pypiからインストールした場合は、おそらくTastypie9.10を実行しています。pypiバージョンのBundleオブジェクトには、実際にはリクエストオブジェクトがありません

django-tastypie masterのgitバージョンにアップグレードしてそれを使用すると、問題は解決するはずです。

pip uninstall django-tastypie 
pip install -e git+https://github.com/toastdriven/django-tastypie.git#egg=django-tastypie
于 2011-11-25T04:46:34.273 に答える