Commit 7b7b7cee by Florian Apolloner

Merge pull request #181 from rizumu/get-prefetch-queryset

Get prefetch queryset
parents a35236b2 15407771
...@@ -289,13 +289,13 @@ class _TaggableManager(models.Manager): ...@@ -289,13 +289,13 @@ class _TaggableManager(models.Manager):
def is_cached(self, instance): def is_cached(self, instance):
return self.prefetch_cache_name in instance._prefetched_objects_cache return self.prefetch_cache_name in instance._prefetched_objects_cache
def get_query_set(self): def get_queryset(self):
try: try:
return self.instance._prefetched_objects_cache[self.prefetch_cache_name] return self.instance._prefetched_objects_cache[self.prefetch_cache_name]
except (AttributeError, KeyError): except (AttributeError, KeyError):
return self.through.tags_for(self.model, self.instance) return self.through.tags_for(self.model, self.instance)
def get_prefetch_query_set(self, instances, queryset = None): def get_prefetch_queryset(self, instances, queryset=None):
if queryset is not None: if queryset is not None:
raise ValueError("Custom queryset can't be used for this lookup.") raise ValueError("Custom queryset can't be used for this lookup.")
...@@ -314,7 +314,7 @@ class _TaggableManager(models.Manager): ...@@ -314,7 +314,7 @@ class _TaggableManager(models.Manager):
source_col = fk.column source_col = fk.column
connection = connections[db] connection = connections[db]
qn = connection.ops.quote_name qn = connection.ops.quote_name
qs = self.get_query_set().using(db)._next_is_sticky().filter(**query).extra( qs = self.get_queryset().using(db)._next_is_sticky().filter(**query).extra(
select = { select = {
'_prefetch_related_val' : '%s.%s' % (qn(join_table), qn(source_col)) '_prefetch_related_val' : '%s.%s' % (qn(join_table), qn(source_col))
} }
...@@ -325,8 +325,9 @@ class _TaggableManager(models.Manager): ...@@ -325,8 +325,9 @@ class _TaggableManager(models.Manager):
False, False,
self.prefetch_cache_name) self.prefetch_cache_name)
# Django 1.6 renamed this # Django < 1.6 uses the previous name of query_set
get_queryset = get_query_set get_query_set = get_queryset
get_prefetch_query_set = get_prefetch_queryset
def _lookup_kwargs(self): def _lookup_kwargs(self):
return self.through.lookup_kwargs(self.instance) return self.through.lookup_kwargs(self.instance)
...@@ -354,11 +355,11 @@ class _TaggableManager(models.Manager): ...@@ -354,11 +355,11 @@ class _TaggableManager(models.Manager):
@require_instance_manager @require_instance_manager
def names(self): def names(self):
return self.get_query_set().values_list('name', flat=True) return self.get_queryset().values_list('name', flat=True)
@require_instance_manager @require_instance_manager
def slugs(self): def slugs(self):
return self.get_query_set().values_list('slug', flat=True) return self.get_queryset().values_list('slug', flat=True)
@require_instance_manager @require_instance_manager
def set(self, *tags): def set(self, *tags):
...@@ -375,7 +376,7 @@ class _TaggableManager(models.Manager): ...@@ -375,7 +376,7 @@ class _TaggableManager(models.Manager):
self.through.objects.filter(**self._lookup_kwargs()).delete() self.through.objects.filter(**self._lookup_kwargs()).delete()
def most_common(self): def most_common(self):
return self.get_query_set().annotate( return self.get_queryset().annotate(
num_times=models.Count(self.through.tag_relname()) num_times=models.Count(self.through.tag_relname())
).order_by('-num_times') ).order_by('-num_times')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment