如何使 Django 应用程序可插拔?
举例来说,我有一个博客应用程序,我希望能够将其放入不同的项目中,但我总是希望该博客与其他模型相关联。 我可能希望它与用户关联:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有几个重要的细节可以确保应用程序可以重用,我认为最好链接到有关该主题的两组更重要的文档:
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:
您可能想研究一下 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/
通用关系允许您拥有外键到任何其他模型。 但是,从您的问题中不清楚您希望外键链接到什么类型的对象。 我怀疑外键关系并不是真正通用的 - 您只是没有发现系统的另一部分也可以是可重用的应用程序。
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.