用于模型访问的 Django DRY URL
读者文摘版本:如何通过获取模型名称的 URL 参数来从模型获取数据(如果指定,则为单行或完整表),而不需要对 URLconf 进行硬编码以匹配每个模型?有关更多详细信息,请继续阅读:
我正在制作一个具有三个模型的应用程序,我想制作一个简单的视图,该视图采用模型的名称,并吐出模型的默认管理器 Model.manager.all( ),
如果存在 slug,则为与该 slug 匹配的单个对象。我不知道如何做到这一点,所以我遇到了必须为每个模型单独制作视图/URLconf 的问题。
URLconf 如下所示:
(r'^model1/$', 'model1_index_view', 'model1_index'),
(r'^model1/(?P<slug>[-\w]+)/$', 'model1_detail_view', 'model1_detail'),
(r'^model2/$', 'model2_index_view', 'model2_index'),
它持续的时间有点长,但我想您已经明白了。我最终对相对大量的 URLconf 进行硬编码,以完成一些我认为可以使用一个视图(将模型名称作为参数和可选的 slug)来完成的事情。我担心的是,如果有人指定模型名称,例如...用户,会发生什么?是否有一段代码可以从应用程序获取模型列表,并确保它与其中一个匹配,而不是与 contrib.auth 或其他应用程序中的模型匹配?
Reader's Digest version: How do I get data (either single row if specified or full table) from a model by taking a URL argument of the model's name without hardcoding the URLconfs to match each model? For more details, read on:
I'm making an app that has three models, and I want to make a simple view that takes a name of a model, and spit out the model's default manager's Model.manager.all(),
and if there's a slug, a single object matching that slug. I don't know how to do this, so I'm running into problems with having to make views/URLconfs for EACH model individually.
Here's what the URLconfs are looking like:
(r'^model1/
It goes on a little bit longer, but I think you get the picture. I'm ending up hardcoding a relatively large amount of URLconfs to do something that I'm thinking I can do with perhaps one View that takes a Model name as an argument and optionally a slug. My concern is, what happens if someone specifies a Model name of say... User? Is there a snippet of code to get a list of models from an app and make sure it matches one of 'em and not a model from contrib.auth or another app?
, 'model1_index_view', 'model1_index'),
(r'^model1/(?P<slug>[-\w]+)/
It goes on a little bit longer, but I think you get the picture. I'm ending up hardcoding a relatively large amount of URLconfs to do something that I'm thinking I can do with perhaps one View that takes a Model name as an argument and optionally a slug. My concern is, what happens if someone specifies a Model name of say... User? Is there a snippet of code to get a list of models from an app and make sure it matches one of 'em and not a model from contrib.auth or another app?
, 'model1_detail_view', 'model1_detail'),
(r'^model2/
It goes on a little bit longer, but I think you get the picture. I'm ending up hardcoding a relatively large amount of URLconfs to do something that I'm thinking I can do with perhaps one View that takes a Model name as an argument and optionally a slug. My concern is, what happens if someone specifies a Model name of say... User? Is there a snippet of code to get a list of models from an app and make sure it matches one of 'em and not a model from contrib.auth or another app?
, 'model2_index_view', 'model2_index'),
It goes on a little bit longer, but I think you get the picture. I'm ending up hardcoding a relatively large amount of URLconfs to do something that I'm thinking I can do with perhaps one View that takes a Model name as an argument and optionally a slug. My concern is, what happens if someone specifies a Model name of say... User? Is there a snippet of code to get a list of models from an app and make sure it matches one of 'em and not a model from contrib.auth or another app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 get_model:
然后在 url 中:
Use get_model:
Then in urls: