我正在尝试向现有模型添加CommentModelMixin
,但运行makemigrations
时django没有收到更改。相反,我得到no changes detected
。如果我向同一模型添加字段,则会检测到更改并创建迁移文件。
django应该选择将mixin添加到makemigrations
中的现有模型吗?
编辑:根据评论添加mixin
class CommentModel(FullModelMixin, FullManagerMixin):
comment = models.TextField()
thread = models.ForeignKey('ops.CommentModel', null=True)
content_type = models.ForeignKey(ContentType)
instance_pk = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'instance_pk')
def __str__(self):
if len(self.comment) > 10:
return self.comment[:10] + '...'
return self.comment
class CommentModelMixin(models.Model):
comments = GenericRelation(
CommentModel,
object_id_field='instance_pk'
)
def add_comment(self, comment, thread=None):
comm = ContentType.objects.get_for_model(self)
CommentModel.objects.create(
instance_pk=self.pk,
content_type=comm, comment=comment, thread=thread
)
class Meta:
abstract = True