django / django-contrib-comments

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do I create a new comment programmatically?

jimkring opened this issue · comments

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!

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