Login

Simple Signal to denormalize vote counts in django-voting

Author:
ericmoritz
Posted:
September 26, 2008
Language:
Python
Version:
1.0
Tags:
voting denormalization
Score:
1 (after 1 ratings)

This attaches a signal to the save and delete signals for the Vote object and recalculates the score for the object and stores it in that object's vote_score attribute.

This allows you to have a list of those objects and not have to calculate in the database the vote score for each object.

1
2
3
4
5
6
7
8
9
# Register some signal handlers
def denormalize_votes(sender, instance, created=False, **kwargs):
    """This recalculates the vote total for the object
    being voted on"""
    instance.object.vote_score = Vote.objects.get_score(instance.object)['score']
    instance.object.save()

models.signals.post_save.connect(denormalize_votes, sender=Vote)
models.signals.post_delete.connect(denormalize_votes, sender=Vote)

More like this

  1. Callable object as signal receiver by diverman 6 years, 4 months ago
  2. Generic Recievers For Signals by ericmoritz 7 years, 1 month ago
  3. Binding signals to abstract models by andreterra 3 years, 2 months ago
  4. Signal to resize uploaded Fiber images as new thumbnailed images by versae 3 years, 5 months ago
  5. Signal to notify new saved comments by arthurfurlan 6 years, 2 months ago

Comments

Please login first before commenting.