Skip to content

Instantly share code, notes, and snippets.

@nathanborror
Forked from tmc/gist:88958
Created April 2, 2009 00:09
Show Gist options
  • Save nathanborror/88959 to your computer and use it in GitHub Desktop.
Save nathanborror/88959 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