python - 如何制作 django 模型 "commentable", "likeable"和 "rateable"

标签 python django mixins

我正在为一个项目使用 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 是一个通用模型,它允许您使用它们的 app_labelmodel_name 跟踪包含在 INSTALLED_APPS 中的所有模型> 和 pk。它的工作方式很简单:

你的通用评论模型

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)
    # ...

您的可重用通用关系模型。最好的方法是使用抽象模型类或混合。例如,使用抽象模型:

from django.db import models
from django.contrib.contenttypes.fields import GenericRelation


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

    class Meta:
        abstract = True

您的可评论模型:

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/

相关文章:

python - 无法运行 Popen.communicate() 两次。 (Python 子进程模块)

python - 存储日期 <= 最后 24 小时时 Django 返回计数

python - 从谷歌导入Create_Service ModuleNotFoundError : No module named 'Google'

C++ Mixins——这是正确的实现方式吗?

inheritance - scala中的自类型继承

Python:添加元素以在字典中设置值

python - 协同多重继承问题

Python 值错误 : cannot copy sequence to array

javascript - Django - 使用 jQuery 将模板加载到变量中

for-loop - LESS:使用存储在数组(或类似的东西)中的数据进行循环