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です。具体的には、次の 3 つのフィールドです。

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 に答える