简体   繁体   English

在 Django 中创建“评论”实例以进行迁移?

[英]Creating “Comment” instances in Django for migration?

I currently have a bunch of old comments, that I need to migrate to django.contrib.comment and the plan was to manually create instances of Comment and then save it as follows:我目前有一堆旧评论,我需要迁移到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()

The main issue is the missing information found in class BaseCommentAbstractModel found in django.contrib.comment.model .主要问题是在 django.contrib.comment.model 中找到的django.contrib.comment.model BaseCommentAbstractModel中缺少信息。 Specifically the three fields:具体三个字段:

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")

I read the documentation and, to the best of my ability, the source, but it wasn't detailed enough.我阅读了文档,并尽我所能阅读了源代码,但还不够详细。 How do I properly specify those fields from the model object (model.Blog)?如何正确指定 model object (model.Blog) 中的这些字段?

Maybe there is a method somewhere that accepts model object and the content of the comment to add?也许某处有一种方法可以接受 model object 和要添加的评论内容?

From the documentation :文档中:

  • set the content_type to an instance of ContentType of your model (the one you're attaching the comment to):content_type设置为您的 model 的ContentType实例(您要附加评论的那个):

    content_type = ContentType.objects.get_for_model(Blog)

  • set object_pk to the primary key of your object:object_pk设置为 object 的主键:

    object_pk = myBlog_instance.pk

  • content_object will point to these 2 fields, you dont have to set it. content_object将指向这两个字段,您不必设置它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM