Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django Tastypie-除了Id之外还使用slug_Django_Tastypie - Fatal编程技术网

Django Tastypie-除了Id之外还使用slug

Django Tastypie-除了Id之外还使用slug,django,tastypie,Django,Tastypie,我想同时使用slug和id来访问我的资源,这样下面的url都会指向同一个资源 http://site.com/api/resource/this-is-the-slug http://site.com/api/resource/35 我已将以下内容添加到我的资源中 prepend_urls(self): return [ url(r"^(?P<resource_name>%s)/(?P<slug>[\w\d_.-]+)/$" % self._me

我想同时使用slug和id来访问我的资源,这样下面的url都会指向同一个资源

http://site.com/api/resource/this-is-the-slug
http://site.com/api/resource/35
我已将以下内容添加到我的资源中

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"),
    ]
prepend_url(self):
返回[
url(r“^(?P%s)/(?P[\w\d.-]+)/$%self.\u meta.resource\u name,self.wrap\u view('dispatch\u detail'),name=“api\u dispatch\u detail”),
]
这使得slug url工作正常,但不幸地破坏了id url。
如何让它们一起工作?

对于同一视图,始终可以有两个目标 而且,slug从来没有
\uu

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"),

    ]
prepend_url(self):
返回[
url(r“^(?P%s)/(?P[\w\d-]+)/$%self.\u meta.resource\u name,self.wrap\u view('dispatch\u detail'),name=“api\u dispatch\u detail”),
url(r“^(?P%s)/(?P[\d]+)/$%self.\u meta.resource\u name,self.wrap\u view('dispatch\u detail'),name=“api\u dispatch\u detail”),
]

我已经试过了,但它对我不起作用。当我尝试使用id时,我得到一个404-我假设因为第一个url使用id并将其用作slug.Ah-但是如果我颠倒url的顺序,它确实有效。。。不知道为什么我没早点发现,谢谢你的帮助!哦,这是因为它与第一个案例相匹配,并且没有通过id找到一个鼻涕虫,因此404我应该抓到它