While working on a ticket system for a client, I realized it probably would make most sense (for various reasons) to leave certain auto-generated information directly in the comment thread itself. Â Code reuse and a single loci of focus for real-time feedback sounded like a winning proposition to me.
Unfortunately, running a Google search for anything in regards to directly using the comments model turned up nothing at all. Â I suspected it was because no one dared abuse Django in a likewise manner; or because it was trivial. Â Having seen the answer, I’m guessing it’s a bit of both.
The main clue that makes it easy is in the django / contribs / comments / forms file.
def get_comment_create_data(self):
"""
Returns the dict of data to be used to create a comment. Subclasses in
custom comment apps that override get_comment_model can override this
method to add extra fields onto a custom comment model.
"""
return dict(
content_type = ContentType.objects.get_for_model(self.target_object),
object_pk = force_unicode(self.target_object._get_pk_val()),
user_name = self.cleaned_data["name"],
user_email = self.cleaned_data["email"],
user_url = self.cleaned_data["url"],
comment = self.cleaned_data["comment"],
submit_date = datetime.datetime.now(),
site_id = settings.SITE_ID,
is_public = True,
is_removed = False,
)
The first two values are for the item you want to have the comment applied against. Â In my case:
# obj = the object we want to save.
from django.contrib import comments
from django.contrib.contenttypes.models import ContentType
from datetime import datetime
auto_comment = comments.models.Comment()
auto_comment.content_type = ContentType.objects.get_for_model(obj)
auto_comment.object_pk = unicode(obj.id) # I don't need force_unicode for my case, but you may.
auto_comment.user_name = 'system comment'
auto_comment.user_email = 'jawaad.mahmood@example.com'
auto_comment.user_url = 'http://it_website/'
auto_comment.submit_date = datetime.now()
auto_comment.site_id = 1 # This may be differently configured in your settings.SITE_ID file
auto_comment.save()
This should save your comment in the appropriate thread.