Skip to content

Instantly share code, notes, and snippets.

@tmc
Created April 2, 2009 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tmc/88958 to your computer and use it in GitHub Desktop.
Save tmc/88958 to your computer and use it in GitHub Desktop.
def comment_saved_handler(sender, instance, created, **kwargs):
"""
Signal handler to populated comment count fields.
Bound to 'comment_count' field, should probably be checking for a field annotation in the model.
"""
if 'comment_count' in [f.name for f in instance.content_object._meta.fields]:
instance.content_object.count = instance.content_object.comments.count()
instance.content_object.save()
from django.contrib.comments.models import Comment
post_save.connect(comment_saved_handler, sender=Comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment