X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/0e9ce396fab5cdfc2f1806bae43f05de77cb1e21..dfd584e3b136d770bf56569030d10712a8722569:/apps/newtagging/models.py diff --git a/apps/newtagging/models.py b/apps/newtagging/models.py index 3f666812c..7e9936f30 100644 --- a/apps/newtagging/models.py +++ b/apps/newtagging/models.py @@ -104,6 +104,7 @@ class TagManager(models.Manager): of field lookups to be applied to the given Model as the ``filters`` argument. """ + # TODO: Do we really need this filters stuff? if filters is None: filters = {} queryset = model._default_manager.filter() @@ -158,18 +159,15 @@ class TaggedItemManager(models.Manager): """ queryset, model = get_queryset_and_model(queryset_or_model) tags = self.tag_model.get_tag_list(tags) - tag_count = len(tags) - if not tag_count: + if not tags: # No existing tags were given return queryset.none() - elif tag_count == 1: - # Optimisation for single tag - fall through to the simpler - # query below. - return queryset.filter(tag_relations__tag=tags[0]) # TODO: presumes reverse generic relation - return queryset.filter(tag_relations__tag__in=tags - ).annotate(count=models.Count('pk')).filter(count=len(tags)) + # Multiple joins are WAY faster than having-count, at least on Postgres 9.1. + for tag in tags: + queryset = queryset.filter(tag_relations__tag=tag) + return queryset def get_union_by_model(self, queryset_or_model, tags): """