Creating backlinks in get_resource_uri

if anyone reads Tastypie-Mailinglist: I didn’t get an answer there, so sorry for crossposting here.

In Tastypie, I changed the resource URL pattern because I use a different key than PK. This works fine when accessing a resource. Now I want to nest this resource in the parent resource, but the nested resource contains the URI with PK, not my user key. I found out that in my case I have to change the get_resource_uri child.

The method in my child resource (which is NamespacedResource) is as follows:

def get_resource_uri(self, bundle_or_obj):

    obj = bundle_or_obj.obj if isinstance(bundle_or_obj, Bundle) else bundle_or_obj

    kwargs={
        'resource_name': self._meta.resource_name,
        'custom_id': obj.custom_id
        }

    return self._build_reverse_url('api_dispatch_detail', kwargs=kwargs)

The child URL redefinition method is as follows:

def override_urls(self):
    return [
        url(r"^(?P<resource_name>%s)/(?P<custom_id>[-_\w\d]+)%s$" % (
                self._meta.resource_name,
                trailing_slash()
            ),
            self.wrap_view('dispatch_detail'),
            name="api_dispatch_detail"
        ),
    ]

But the application cannot cancel the url. I get this error:

Reverse for 'api_dispatch_detail' with arguments '()' and keyword arguments '{'custom_id': u'3_ee5-4423', 'resource_name': 'myresource'} not found.

How to change url correctly?

Thanks in advance.

+5
source share
1

URL- resource_name api_name kwargs.

kwargs :

kwargs = {
    'api_name': 'v1',  # Or whatever you have set for your api
    'resource_name': self._meta.resource_name,
    'custom_id': obj.custom_id
}
+2

All Articles