我目前有一堆旧评论,我需要迁移到django.contrib.comment
,计划是手动创建Comment实例,然后按如下方式保存:
# assume some_content is NOT a django Comment instance, but in some proprietary format
# assume the model I'm attaching the comment to is called Blog i.e models.Blog
c = Comment()
c.user = user
c.submit_date = some_comment.comment_date_time
c.comment = some_comment.comment
...
c.save()
主要问题是BaseCommentAbstractModel
中找到的课程django.contrib.comment.model
中缺少的信息。特别是三个领域:
BaseCommentAbstractModel(models.Model):
# Content-object field
content_type = models.ForeignKey(ContentType,
verbose_name=_('content type'),
related_name="content_type_set_for_%(class)s")
object_pk = models.TextField(_('object ID'))
content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")
我阅读了文档,并尽我所能来源,但它不够详细。如何从模型对象(model.Blog)中正确指定这些字段?
也许有一个方法可以接受模型对象和要添加的注释的内容?
答案 0 :(得分:1)
set the content_type to an instance of ContentType of your model (the one you're attaching the comment to):
content_type = ContentType.objects.get_for_model(Blog)
set object_pk to the primary key of your object:
object_pk = myBlog_instance.pk
content_object will point to these 2 fields, you dont have to set it.
content_type = ContentType.objects.get_for_model(Blog)