[Answered ]-Django-tastypie multiple urls in prepend_urls

2👍

I’ve solved this, thanks to Zeograd’s suggestion about regexp.

I changed the first {ID} expression to match on an integer and the second on a string, like so:

def prepend_urls(self):
        return [
            url(
                r"^(?P<resource_name>%s)/(?P<my_id>\d+)/$"
                % self._meta.resource_name, self.wrap_view('dispatch_detail'),
                name="api_dispatch_detail_id"),
            url(
                r"^(?P<resource_name>%s)/(?P<name>[\w\d_.-]+)/$"
                % self._meta.resource_name, self.wrap_view('dispatch_detail'),
                name="api_dispatch_detail_name"),
        ]
👤phalt

0👍

your 2 regexp are matching the same input url, as they’re processed in order, the 2nd never matches.
You may want to use an intermediate url fragment to distinguish the 2, like

r"^(?P<resource_name>%s)/by-name/(?P<name>[\w\d_.-]+)/$"

for the second

Leave a comment