我在一个项目中使用 Django 2.0.8 和 Python 3.5。我的项目中有不同的模型,其中一些模型,我想允许评论 - 对象(例如博客文章)和对博客文章的评论都是讨人喜欢的。

我正在使用 threaded comments django app 来提供评论功能。

假设我有一个模型 Foo(见下文):

from django.db import models
from django.conf import settings

class Foo(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, blank=False, null=False, default=1, on_delete = models.PROTECT)
    # ...


class Likeable():
    pass

class Rateable():
    pass

我如何使用 mixins(或任何其他机制),使对象 Foo“可评论”(即可以评论的对象)、“喜欢的”(即可以评论的对象)和“可评价的” "(即可以评分的对象?)- 记住,对对象的评论可能会同时被喜欢和评分。

最佳答案

根据 django documentation ,您可以使用 内容类型框架 来实现这一点。 ContentType 是一个通用模型,它允许您使用 INSTALLED_APPSapp_labelmodel_name 跟踪 pk 中包含的所有模型。它的工作方式很简单:

您的通用 Comment 模型

from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.conf import settings


class Comment(models.Model):
    # Generic relation fields
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
    # Model specific fields
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE
    )
    comment = models.TextField()
    created = models.DatetimeField(auto_now_add=True)
    # ...

您可重用的通用关系模型。最好的方法是使用抽象模型类或 mixin。例如,使用抽象模型:
from django.db import models
from django.contrib.contenttypes.fields import GenericRelation


class Commentable(models.Model):
    comments = GenericRelation(Comment)

    class Meta:
        abstract = True

您的 Commentable 模型:
from django.db import models


class Foo(Commentable, Likeable, ...):
    # your stuff

如何使用它:
# Add a new comment to Foo
foo = new Foo()
foo.save()
foo.comments.create(author=author, comment="Your comment")

# Retrieve all comments from an specific user no matter the base model
comments = Comment.objects.filter(author=author)

编辑 正如@ozren1983 所说,每种方法都有自己的缺点,但这是标准的做法。

主要优点是:
  • 您可以在一个查询中检索所有可评论模型中的所有评论(例如)。使用为每个模型添加评论、赞等表的方法,您需要连接每个模型的查询。例如,如果您有很多模型,或者如果您想合并结果并对其进行排序,这可能会出现问题并且有点挑战。
  • 每个功能(评论、喜欢)只有一个表意味着在发生变化时只有一个数据库迁移。如果您的数据库很大,这可能是关键。

  • 主要缺点是缺乏对数据库中这种通用关系的完整性检查。但是,如果您打算严格使用 django ORM ,则不应破坏任何内容。

    奖励: 许多项目使用的另一种方法是从称为 ItemThread 的特定模型继承模型(一对一关系)。然后,您可以向该模型添加所有评论、喜欢等功能。这称为多表继承。一个例子:
    from django.db import models
    
    
    class Thread(models.Model):
        pass
    
    class Comment(models.Model):
        # Relation with thread
        thread = models.ForeignKey(
            Thread,
            on_delete=models.CASCADE,
            related_name="comments"
        )
        # Model specific fields
        author = models.ForeignKey(
            settings.AUTH_USER_MODEL,
            on_delete=models.CASCADE
        )
        comment = models.TextField()
        created = models.DatetimeField(auto_now_add=True)
        # ...
    
    class Foo(Thread):
        pass
    

    与使用通用关系不同,这种方法的主要优点是,通过这种方式,您可以进行数据库完整性检查。

    主要缺点是您的数据库结构可能会变得复杂。

    关于python - 如何制作 django 模型 "commentable"、 "likeable"和 "rateable",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51824934/

    10-14 11:57
    查看更多