【问题标题】:django_comments adding new field in the modeldjango_comments 在模型中添加新字段
【发布时间】:2016-08-11 19:07:25
【问题描述】:

我正在尝试在 django_comment 模型中添加一个新字段。根据文档,大多数自定义评论模型将继承 CommentAbstractModel 模型:

from django.db import models
from django_comments.models import CommentAbstractModel

class CommentWithTitle(CommentAbstractModel):
   title = models.CharField(max_length=300)

如果我生成迁移,则它将所有字段添加到迁移中(评论模型中的所有字段加上标题字段)。

在运行迁移之后,CommentWithTitle 表和django_comments 表被创建。但是django_comments 将毫无用处(未使用)。

另一种方法是这样生成表格:

from django_comments.models import Comment

class CommentWithTitle(Comment):
    title = models.CharField(max_length=300)

它仅使用comment_ptr 的引用生成一个字段的迁移。

我的问题是:哪种方法更好?我认为第一个模型很好,因为它在一个表中包含所有字段。但这会生成根本不使用的django_model

【问题讨论】:

    标签: django django-comments


    【解决方案1】:

    我会遵循文档。

    查看实现,Comment 基本上只是扩展 CommentAbstractModel 并指定了 db_table

    class Comment(CommentAbstractModel):
        class Meta(CommentAbstractModel.Meta):
            db_table = "django_comments"
    

    我怀疑如果您执行您提到的第二个选项,迁移会引发错误,因为 db_table 将被创建两次。

    【讨论】:

      猜你喜欢
      • 2020-04-20
      • 2018-10-16
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 2019-06-26
      • 2021-12-09
      • 1970-01-01
      • 2020-11-04
      相关资源
      最近更新 更多