0

我目前有一堆旧评论,我需要迁移到django.contrib.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)中正确指定这些字段?

也许某处有一种方法可以接受模型对象和要添加的评论内容?

4

1 回答 1

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.

于 2011-06-08T14:14:32.973 回答