django / django-contrib-comments

BSD 3-Clause "New" or "Revised" License
614 stars 196 forks source link

How do I create a new comment programmatically? #155

Closed jimkring closed 4 years ago

jimkring commented 4 years ago

I'd like to create a new comment programatically. My use case is that I'd like to automatically create the first comment for a blog_post when the blog_post is first created.

I have the comment text, the user object, and the model instance. Can I do something like:

new_comment = Comment( comment="Some Text", user=some_user, object=some_object )

Would something like this work? Thanks!

claudep commented 4 years ago

Use content_object=..., as object is a generic foreign key (https://docs.djangoproject.com/en/3.1/ref/contrib/contenttypes/#generic-relations)