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

1

7

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)
Splenitis answered 1/3, 2013 at 11:50 Comment(0)
A
14

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.

Abdella answered 1/3, 2013 at 14:5 Comment(2)
Thanks to David R. for explaining how to build a bundle & adding an example!Abdella
I stumbled upon this answer while searching for same error message for obj_get(). This answer holds true obj_get() as wellGratuitous

© 2022 - 2024 — McMap. All rights reserved.