Tastypie嵌套资源 - cached_obj_get()只需要2个参数(1个给定) [英] Tastypie Nested Resources - cached_obj_get() takes exactly 2 arguments (1 given)
问题描述
我正在尝试使用此示例: http: //django-tastypie.readthedocs.org/en/latest/cookbook.html#nested-resources
I'm trying to use the example here: http://django-tastypie.readthedocs.org/en/latest/cookbook.html#nested-resources
由于某种原因我得到:
cached_obj_get()只需要2个参数(1个)
cached_obj_get() takes exactly 2 arguments (1 given)
即使我明确地用2个参数来表示(就像上面的例子一样)
这是我的代码:
even though i clearly call it with 2 arguments (exactly like in the aforementioned example. this is my code:
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/feed%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_feed'), name="api_get_feed"),
]
def get_feed(self, request, **kwargs):
try:
obj = self.cached_obj_get(request=request, **self.remove_api_resource_names(kwargs))
except ObjectDoesNotExist:
return HttpGone()
except MultipleObjectsReturned:
return HttpMultipleChoices("More than one resource is found at this URI.")
feed_resource = FeedItemResource()
return feed_resource.get_list(request, p_id=obj.id)
推荐答案
对不起,感到困惑 - 有一个 API更改以改进授权,这更改了签名 cached_obj_get
from:
Sorry for the confusion - there was an API change to improve authorization which changed the signature for cached_obj_get
from:
def cached_obj_get(self, request=None, **kwargs):
至
def cached_obj_get(self, bundle, **kwargs):
这个变化是一致的 - 如果您需要 r equest
对象,它可用作 bundle.request
- 但显然文档需要更新。
This change is consistent going forward – and if you needed the request
object, it's available as bundle.request
– but obviously the documentation needs to be updated.
您可以使用以下内容构建捆绑对象:
You can build a bundle object with:
basic_bundle = self.build_bundle(request=request)
然后使用它作为参数 cached_obj_get
(见 Resource.get_detail
源代码作为示例):
then use it as an argument to cached_obj_get
(see Resource.get_detail
source code as an example):
obj = self.cached_obj_get(bundle=basic_bundle, **self.remove_api_resource_names(kwargs))
另一个令人困惑的方面,如果你不熟悉Python的对象模型该方法总是接收至少一个参数,因为第一个位置参数始终为对象实例或 self
并且关键字参数不包括在该计数中,因此1给定表示该方法仅在 self c
和束
。
The other confusing aspect if you're not familiar with Python's object model is that methods always receive at least one argument because the first positional argument is always the object instance or self
and keyword arguments aren't included in that count so "1 given" means that the method only received the self
positional argument when it was expecting self
and bundle
.
这篇关于Tastypie嵌套资源 - cached_obj_get()只需要2个参数(1个给定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!