Django 松耦合并扩展预先存在的模型
假设我创建了一个应用程序博客 [这是不相关的,它就在那里,所以我可以执行 app.blog.posts - 所以它实际上只是一个文件夹],然后在博客中我在博客内创建应用程序帖子,帖子定义模型帖子。执行此操作后,我将在博客中创建一个应用程序类别,该类别应该扩展模型帖子(或任何类似帖子的模型 - 将类别字段添加到帖子),然后创建它自己的表。如果这些应用程序完全不相关(例如:django-tags),类别如何能够在最终不知情的情况下扩展帖子?我对 django 松耦合有点困惑,所以也许有人可以向我解释一下(除非已经有例子,但我找不到遵循这种思维方式的好例子。)
Lets say that I create an application blog [which is irrelevant it's just there so I can do app.blog.posts - so it's really just a folder] and then in blog I create the app posts inside of blog and posts defines the model post. After I do this I would then create an application categories in blog which should extend the model posts (or any model like posts -- adding the category field to post) and then creating it's own table. If these applications were entirely unrelated (exg: django-tags) how would categories be able to extend post without ultimately being aware of it? I am a bit confused about django loose coupling so perhaps somebody can explain it to me please (unless there are already examples but I couldn't find a good example that follows this style of thinking.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果“扩展”意味着以某种方式添加功能,那么一般来说, 信号 是好主意。使用信号,您可以挂钩模型的某些方法,甚至无需修改模型文件。
如果您只需要博客文章有类别,那么在博客文章中使用外键会有什么问题?
设计尽可能通用的 django 应用程序总是一个好主意,这意味着,如果它可能会被重用,请将其作为自己的应用程序。
If "extend" means to somehow add functionality, in general, signals are are great idea. Using signals, you can hook into some of a model's methods without even modifying the model files.
If you simply need your blog posts to have categories, what's the problem using a foreign key in blog posts?
It's always a good idea to design django apps as generic as possible, means, make it an own app if it's likely going to be reused.