简体   繁体   English

Django 常用注释 model 在不同的应用程序中使用

[英]Django common comment model to use in different apps

I am creating a project that requires comments in multiple apps like blog , wikis , and pages .我正在创建一个需要多个应用程序(如blogwikispages )中的评论的项目。 I have a commons app that contains all common models.我有一个包含所有常见模型的commons应用程序。 How do I have a Comment model that's common to all apps?我如何拥有所有应用程序共有的Comment model?

commons/models.py公共/模型.py

class Comment(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comments')
    body = models.TextField()
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True)

blog/models.py博客/models.py

class Post(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
    content = models.TextField()

wikis/models.py维基/models.py

class Wiki(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='wikis')
    content = models.TextField()

From what I researched, I have the following options,根据我的研究,我有以下选择,

  1. Three Comment models three apps.三个Comment模型三个应用程序。
  2. One Comment model in commons app with ForeignKey relations to the other apps (which I believe will cause circular import issues) and end up with a comment table with multiple columns like blog_id , wiki_id , and page_id . commons应用程序中的一条Comment model 与其他应用程序的 ForeignKey 关系(我相信这会导致循环导入问题)并最终得到一个包含多个列的评论表,如blog_idwiki_idpage_id
  3. Use Django's GenericForeignKey relationships.使用 Django 的GenericForeignKey关系。

I don't want to do 3. But out of 1 and 2 I would like to know which is the most efficient way to handle this without repeating codes and adding unnecessary database joins.我不想做 3。但是在 1 和 2 中,我想知道在不重复代码和添加不必要的数据库连接的情况下,哪种方法是处理此问题的最有效方法。

Or is there a better way to do this?还是有更好的方法来做到这一点?

It's been a while since you asked this question so I'd love to know what conclusions you came to.你问这个问题已经有一段时间了,所以我很想知道你得出了什么结论。

One idea would be to create an abstract model in the commons app and then app-specific sub-classes in each app.一个想法是在公共应用程序中创建一个抽象 model,然后在每个应用程序中创建特定于应用程序的子类。 However, this violates the portability principle since the app will now be dependant upon the commons app.但是,这违反了可移植性原则,因为该应用程序现在将依赖于公共应用程序。

And therein lies the problem with the idea of a commons app that would contain all the models shared by other apps.这就是commons应用程序包含其他应用程序共享的所有模型的想法的问题。 While we might consider this to solve the issue of circular references, they are just the symptom of a larger problem caused by ignoring this principle in the first place.虽然我们可能会考虑这样做来解决循环引用的问题,但它们只是最初忽略这一原则导致的更大问题的症状。 Creating further separation only makes things worse.进一步分离只会让事情变得更糟。

In your project, you seem to have identified 3 separate web applications and so I think the answer should be to create app-specific models in each one... even if it seems to violate the DRY principle because it favours portability.在您的项目中,您似乎已经确定了 3 个单独的 web 应用程序,因此我认为答案应该是在每个应用程序中创建特定于应用程序的模型......即使它似乎违反了 DRY 原则,因为它有利于便携性。

In my opinion (which is always subject to change), no app should be dependent upon anything beyond installed modules and Django itself.在我看来(这总是会发生变化),除了已安装的模块和 Django 本身之外,任何应用程序都不应该依赖于任何东西。 This ensures portability and thus reusability of code.这确保了可移植性,从而确保了代码的可重用性。

I say this knowing that it may result a monolithic application but I feel that this is preferable to circular references caused by cross dependencies.我这样说是知道它可能会导致一个单一的应用程序,但我觉得这比由交叉依赖引起的循环引用更可取。 The only solution to that is to spin off core functionality into modules that could be included in any project.唯一的解决方案是将核心功能分拆成可以包含在任何项目中的模块。 The User module included in Django would be a good example of this. Django 中包含的用户模块就是一个很好的例子。 Something like django-simple-history would be another.像 django-simple-history 这样的东西将是另一个。 Neither one relies on your code... your code relies upon them.没有人依赖你的代码......你的代码依赖于他们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM