0

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:

# 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. 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)?

Maybe there is a method somewhere that accepts model object and the content of the comment to add?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
GoogleDroid
  • 185
  • 1
  • 6

1 Answers1

1

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 = 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.

manji
  • 47,442
  • 5
  • 96
  • 103