2016-08-11 79 views
0

我试图在django_comment模型中添加一个新字段。根据该文件,大多数自定义注释车型将继承该CommentAbstractModel型号:django_comments在模型中添加新字段

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

回答

0

我会按照文档。

看一下实现,Comment基本上只是扩展了CommentAbstractModel而指定了db_table

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

我怀疑,如果你做你提到的第二个选项,则迁移将抛出一个错误,因为db_table将被创建两次。

相关问题