django-taggit: Is there a way to produce less db queries? -


say have model:

class entry(models.model):     ...     tags = taggablemanager() 

when iterate on entry.objects.all() in template, entry.tags.all produces 1 more query database. possible reduce queries number? using select_related() (i know won't work, since django-taggit uses manytomany relation, sure there should way select entries related tags in 1 hit)?

from django 1.4 onward, can use prefetch_related retrieve one-to-many relations on queryset in single query. unfortunately doesn't work brilliantly django-taggit, because 'tags' property manager rather true relation, , prefetch_related can't make sense of it. instead, need follow tagged_items relation:

entries = entry.objects.prefetch_related('tagged_items__tag')

you need go through similar contortions in template code access prefetched tags, because entry.tags.all run query rather making use of prefetch:

{% tagged_item in entry.tagged_items %}     <li>{{ tagged_item.tag.name }}</li> {% endfor %} 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -