简体   繁体   English

如何在 Django 中为帖子模型创建评论、赞、标签、报告等?

[英]How to create comment, like, tag, report, and so on for a post model in Django?

I created a model which name is Post and placed in this path : MyProject/Postapp/models look like this:我创建了一个名为 Post 的模型并放置在此路径中: MyProject/Postapp/models如下所示:

class Post(models.Model):
    user = models.ForeignKey(User, related_name="posts",  
on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now=True)
    message = models.TextField()
    message_html = models.TextField(editable=False)
    group = models.ForeignKey(Group, related_name="posts",null=True, 
blank=True,  on_delete=models.CASCADE)

    def __str__(self):
         return self.message

    def save(self, *args, **kwargs):
        self.message_html = misaka.html(self.message)
        super().save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse(
            "posts:single",
            kwargs={
                "username": self.user.username,
                "pk": self.pk
            }
        )

    class Meta:
        ordering = ["-created_at"]
        unique_together = ["user", "message"]

So in the following, I want to add a comment, ... to each Post which created by Users.所以在下面,我想添加一个评论,......到用户创建的每个帖子。 Should I start a new app (Commentapp, ...) or create models in the current path and add some fields in the Post model?我应该启动一个新应用程序(Commentapp,...)还是在当前路径中创建模型并在 Post 模型中添加一些字段?

The answer is the same as for any "module" in any language (at least in any language that has some concept of modularity): you want strong cohesion (features of a module should be closely related) and low coupling (your module should have as few dependencies as possible, and you specially don't want cyclic dependencies). 答案与任何语言(至少在具有某种模块化概念的任何语言中)的任何“模块”都是相同的:您想要强大的内聚力(模块的功能应紧密相关)和低耦合(模块应具有尽可能少的依赖性,而您特别不希望循环依赖性)。

In your case, Comments are obviously closely related to Posts so it makes perfect sense to keep them together (strong cohesion), while splitting them into different aps would actually add dependencies (and possibly cyclic ones) so it would increase coupling. 在您的情况下, Comments显然与Posts密切相关,因此将它们放在一起(紧密的内聚性)是很有意义的,而将它们拆分为不同的ap实际上会增加依赖性(并可能是循环的),因此会增加耦合。

NB: unless you want a generic comments app that can work with any other model of course (in which case one already exists ), but from experience I would not get into this kind of premature generalization - most often than not you end up not using your "generic" model with anything else so all it buys you is only extra complexity (and quite some of it) for no real benefit. 注意:除非您想要一个可以与其他任何模型一起使用的通用comments应用程序(当然,在这种情况下已经存在 ),但是根据经验,我不会陷入这种过早的概括中-大多数情况下,您最终会不使用您的“通用”模型以及其他任何东西,因此它所带给您的只是额外的复杂性(其中相当一部分),而没有真正的好处。 The mere point that the existing contrib.comments app was finally removed from the django trunk is a good enough sign that this was not a good candidate for a generic reusable app (as far as I'm concerned I used it once on a project, then dumped it and reimplement my own model which worked the way I needed with much less code and much better performances). 仅从Django主干中删除现有的contrib.comments应用程序这一点就足以说明这不是通用可重用应用程序的理想选择(就我所担心的,我曾在项目中使用过一次,然后将其转储并重新实现我自己的模型,该模型以所需的方式以更少的代码和更好的性能工作了。

class Comment(models.Model):
    post = models.ForeignKey('blog.Post', on_delete=models.CASCADE, related_name='comments')
    author = models.CharField(max_length=200)
    text = models.TextField()
    created_date = models.DateTimeField(default=timezone.now)
    approved_comment = models.BooleanField(default=False)

    def approve(self):
        self.approved_comment = True
        self.save()

    def __str__(self):
        return self.text

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

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