Monday, August 30, 2010

Query overhead or not ?

First of all I know of the django-taggit application. But I'm new to django and I thought this would be a good way of getting to know this great framework a little bit better.
That lined out, I have a Post and a Tag model. Each Tag.name property of the tag model is unique.

I'm trying to figure out which Tags are used most.

I do this by the following code:

from homepage.models.blog import Post, Tag

indexed_tags = {}

tags = Tag.objects.all()

for t in tags:
indexed_tags[t] = Post.objects.filter(tags__name=t).count()

Next I would sort indexed_tags and I have a dictionary of which tags are used the most.

I'm wondering if this is a good idea ? Since if I have 1000 tags this would require 1000 queries just for one simple tag cloud.
Is there a better way of solving this without adding an extra count field to the Tag model.


Regards,

Jonas Geiregat
jonas@geiregat.org


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment