Fergal Moran
Fergal Moran

Reputation: 4644

Django Tastypie - using slug IN ADDITION to Id

I want to use both a slug and id to access my resources, so that the following urls would both point to the same resource

http://site.com/api/resource/this-is-the-slug
http://site.com/api/resource/35

I have added the following to my resource

prepend_urls(self):
    return [
        url(r"^(?P<resource_name>%s)/(?P<slug>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
    ]

Which gets the slug url working fine but unfortunately breaks the id url. How do I get both of them working together?

Upvotes: 0

Views: 224

Answers (1)

karthikr
karthikr

Reputation: 99640

You can always have 2 targets for the same view Also, slugs never have . and _

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

    ]

Upvotes: 1

Related Questions