Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 418
itayab81 Avatar asked Mar 01 '13 11:03

itayab81


1 Answers

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.

like image 163
Chris Adams Avatar answered Sep 20 '22 09:09

Chris Adams