首页 > 解决方案 > 如何使 django 模型“可评论”、“可爱”和“可评价”

问题描述

我正在为一个项目使用 Django 2.0.8 和 Python 3.5。我的项目中有不同的模型,我希望允许对其中的一些模型进行评论——对象(例如博文)和对博文的评论都是讨人喜欢的。

我正在使用线程评论 django 应用程序来提供评论功能。

假设我有一个模型 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 “可评论”(即可以评论的对象)、“可爱”(即可以评论的对象)和“可评价” ”(即可以评价的对象?)- 请记住,对对象的评论可能既被喜欢又被评价。

标签: pythondjangomixins

解决方案


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

您的通用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 ,则不应破坏任何内容。


奖励:Item许多项目使用的另一种方法是从名为or的特定模型继承模型(一对一关系)Thread。然后,您可以将所有评论、喜欢等功能添加到此模型中。这称为多表继承。一个例子:

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

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

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


推荐阅读