在Django自己的注释框架django-评语中,如果我创建自己的注释模型如下所示:
from django_comments.models import Comment
class MyCommentModel(Comment):问:我应该如何将这个新的评论模型(**MyCommentModel**)与现有的 Article 模型联系起来?使用属性content_type**,** object_pk 或 content_object
发布于 2021-09-10 11:54:56
你可以像这样直接使用它:
article = Article.objects.get(id=1)
comment = MyCommentModel(content_object=article, comment='my comment')
comment.save()但是,如果要访问Article的注释,可以使用GenericRelation
from django.contrib.contenttypes.fields import GenericRelation
class Article(models.Model):
...
comments = GenericRelation(MyCommentModel)
article = Article.objects.get(id=1)
article.comments.all()https://stackoverflow.com/questions/69130228
复制相似问题