1. offline
  2. django-tagging2

Commits

offline  committed a1f03e6

get_intersection_by_model reimplemented

  • Participants
  • Parent commits 9729cb6
  • Branches default

Comments (0)

Files changed (1)

File newtags/managers.py

View file
 from django.contrib.contenttypes.models import ContentType
-from django.db import connection, models
-from django.db.models import Avg, Max, Min, Count
+from django.db import models
+from django.db.models import Count
+from django.utils.itercompat import groupby
 
 from newtags.models import TaggedItem, Tag
 from newtags.utils import get_tag_list, LOGARITHMIC, calculate_cloud
         return self.get_intersection_by_model(tags)
 
 
-
+    
+    
     def get_intersection_by_model(self, tags):
         """
         Create a ``QuerySet`` containing instances of the specified
         tags = get_tag_list(tags)
         if not tags:
             return self.model._default_manager.none()
-    
-        objects_id = set([i.id for i in self.model._default_manager.all()])
-        for tag in tags:
-            objects_id = objects_id & set([i.object_id for i in TaggedItem.objects.filter(tag=tag)])
+        
+        ctype = ContentType.objects.get_for_model(self.model)
+        items = TaggedItem.objects.filter(tag__in=tags, content_type=ctype).order_by("object_id")
+        
+        objects_id = []
+        
+        for object_id, tag_list in groupby(items, lambda i: i.object_id):
+            if len(list(tag_list)) == len(tags):
+                objects_id.append(object_id)
+        
+        if not objects_id:
+            return self.model._default_manager.none()
+
+
         return self.model._default_manager.filter(pk__in=objects_id)
+
+