如何使 Django 应用程序可插拔?

发布于 2024-07-18 10:02:30 字数 233 浏览 7 评论 0原文

举例来说,我有一个博客应用程序,我希望能够将其放入不同的项目中,但我总是希望该博客与其他模型相关联。 我可能希望它与用户关联:

site.com/someuser/blog

但在另一个网站上,我希望它与学校关联:

site.com/someschool/blog

例如,在一种情况下, 一种使博客应用程序可插入的方法,这样每当我将其放入项目中时就无需重新定义模型(添加外键字段)?

Say for example I have a Blog app that I want to be able to drop into different projects, but I always want the Blog to be associated with some other model. For example, in one case I may want it to be associated with a user:

site.com/someuser/blog

But on another site I want it to be associated with, say, a school:

site.com/someschool/blog

Is there a way to make the Blog app pluggable so that it's not necessary to redefine the model (adding a foreign key field) whenever I drop it into a project?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

小…楫夜泊 2024-07-25 10:02:30

有几个重要的细节可以确保应用程序可以重用,我认为最好链接到有关该主题的两组更重要的文档:

There are several important details for making sure an app can be reusable and I think it's best to link to two of the more important sets of documentation on the topic:

冷弦 2024-07-25 10:02:30

您可能想研究一下 ContentTypes 框架,我用它创建了一个评论应用程序,可用于评论数据库中的任何模型(由于不同的原因,我不想使用标准的 django 评论应用程序)。

http://docs.djangoproject.com/en/dev/ref/contrib /内容类型/

You might want to look into the ContentTypes framework, I used it to create a comment app that can be used for commenting any model in the database (for different reasons, I didn't want to use the standard django comment app).

http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/

神经暖 2024-07-25 10:02:30

通用关系允许您拥有外键到任何其他模型。 但是,从您的问题中不清楚您希望外键链接到什么类型的对象。 我怀疑外键关系并不是真正通用的 - 您只是没有发现系统的另一部分也可以是可重用的应用程序。

Generic relationships allow you to have a foreign key to any other model. However it's not clear from your question what type of object you want a foreign key to link to. I suspect that foreign key relationship isn't really generic - you just haven't spotted another part of your system that could also be a reusable app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文