用于模型访问的 Django DRY URL

发布于 2024-08-16 03:36:19 字数 693 浏览 3 评论 0原文

读者文摘版本:如何通过获取模型名称的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埋葬我深情 2024-08-23 03:36:20

使用 get_model:

from django.db.models import get_model

def my_view(request, model_name, item_slug):
    try:
        model = get_model('app_name', model_name)
    except:
        ## throw an error
        pass
    objects = model.objects.get(slug=item_slug)

然后在 url 中:

 (r'^(?P<model_name>[-\w]+/(?P<slug>[-\w]+)/
, 'model_detail_view', 'model_detail'),

Use get_model:

from django.db.models import get_model

def my_view(request, model_name, item_slug):
    try:
        model = get_model('app_name', model_name)
    except:
        ## throw an error
        pass
    objects = model.objects.get(slug=item_slug)

Then in urls:

 (r'^(?P<model_name>[-\w]+/(?P<slug>[-\w]+)/
, 'model_detail_view', 'model_detail'),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文