from operator import itemgetter
from django.db.models import Manager
from django.db.models.query import QuerySet

from django.contrib.contenttypes.models import ContentType
import itertools

class VoteQuerySet(QuerySet):
    def delete(self, *args, **kwargs):
        """Handles updating the related `votes` and `score` fields attached
        to the model."""
        qs = self.values_list('score', 'content_type', 'object_id')\
                        .order_by('content_type', 'object_id')

        qs = list(qs)
        for ct_type, votes_a in itertools.groupby(qs, itemgetter(1)):
            model = ContentType.objects.get(pk=ct_type).model_class()
            for ct_pk, votes_b in itertools.groupby(votes_a, itemgetter(2)):
                obj = model.objects.get(pk=ct_pk)
                for vote in votes_b:
                    for field in getattr(obj, '_xratings', []):
                        getattr(obj, field.name)._rm_vote(score=vote[0])
                obj.save()

        retval = super(VoteQuerySet, self).delete(*args, **kwargs)
        return retval

class VoteManager(Manager):
    def get_query_set(self):
        return VoteQuerySet(self.model)

    def get_for_user_in_bulk(self, objects, user):
        objects = list(objects)
        if len(objects) > 0:
            ctype = ContentType.objects.get_for_model(objects[0])
            votes = list(self.filter(content_type__pk=ctype.id,
                                     object_id__in=[obj._get_pk_val() \
                                                    for obj in objects],
                                     user__pk=user.id))
            vote_dict = dict([(vote.object_id, vote) for vote in votes])
        else:
            vote_dict = {}
        return vote_dict

@@ -1,12 +1,85 @@
+# coding=utf-8%0Afrom __future__ import unicode_literals%0A%0Aimport itertools%0A
 from operato
@@ -98,16 +98,17 @@
 mgetter%0A
+%0A
 from dja
@@ -180,17 +180,16 @@
 uerySet%0A
-%0A
 from dja
@@ -239,32 +239,16 @@
 entType%0A
-import itertools
 %0A%0Aclass 
@@ -318,19 +318,18 @@
         
-%22%22%22
+# 
 Handles 
@@ -384,16 +384,19 @@
 attached
+ to
 %0A       
@@ -396,18 +396,17 @@
         
-to
+#
  the mod
@@ -412,11 +412,8 @@
 del.
-%22%22%22
 %0A   
@@ -426,16 +426,41 @@
 s = self
+.distinct()%5C%0A            
 .values_
@@ -503,28 +503,16 @@
 t_id')%5C%0A
-            
         
@@ -1136,16 +1136,17 @@
 retval%0A%0A
+%0A
 class Vo
@@ -1433,21 +1433,16 @@
 votes = 
-list(
 self.fil
@@ -1504,21 +1504,16 @@
         
-     
 object_i
@@ -1540,10 +1540,8 @@
 al()
- %5C
 %0A   
@@ -1580,29 +1580,24 @@
             
-     
 for obj in o
@@ -1637,21 +1637,16 @@
         
-     
 user__pk
@@ -1654,17 +1654,16 @@
 user.id)
-)
 %0A       

Update managers.py