Tastypie Nested Resources - cached_obj_get() takes exactly 2 arguments (1 given)

半城伤御伤魂 提交于 2019-12-08 19:18:05

问题


I'm trying to use the example here: http://django-tastypie.readthedocs.org/en/latest/cookbook.html#nested-resources

for some reason i get:

cached_obj_get() takes exactly 2 arguments (1 given)

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)

回答1:


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):

to

def cached_obj_get(self, bundle, **kwargs):

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)

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))

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.



来源:https://stackoverflow.com/questions/15157071/tastypie-nested-resources-cached-obj-get-takes-exactly-2-arguments-1-given

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!