Tag.objects.values('name').annotate(times_used=Count('post__tags')) That's the solution!
Op 1-sep-2010, om 15:00 heeft Jonas Geiregat het volgende geschreven: > I look at aggregation and annotation but I don't think It's possible to solve > this by using any of those two systems. > Or am I overlooking something ? > > > Op 31-aug-2010, om 13:09 heeft Jonas Geiregat het volgende geschreven: > >> Thanks for your advise. I'm short of time right now so I briefly looked at >> django's aggregation possibilities. >> I'll look into them more deeply tonight. >> >> But I stumbled upon a problem. >> >> Post.objects.values('tags') >> >> Gives me: >> >> FieldError: Invalid field name: 'tags' >> >> In [56]: Post.tags >> Post.tags >> >> In [56]: Post.tags >> Out[56]: >> <django.db.models.fields.related.ReverseManyRelatedObjectsDescriptor object >> at 0x78f9f0> >> >> Post.tags does exist. Maybe it's because it's a ManyToManyField ? So I tried >> Post.objects.values('tags__name'). Still got an error ? >> >> I'm wondering why ? >> >> Op 31-aug-2010, om 09:28 heeft Jirka Vejrazka het volgende geschreven: >> >>>> 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. >>> >>> Hi Jonas, >>> >>> your gut feeling was correct - it's not a very good idea, although >>> it might work for a small site. You might want to take a look at >>> database aggregation in Django: >>> http://docs.djangoproject.com/en/1.2/topics/db/aggregation/ >>> >>> Cheers >>> >>> Jirka >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Django users" group. >>> To post to this group, send email to django-us...@googlegroups.com. >>> To unsubscribe from this group, send email to >>> django-users+unsubscr...@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/django-users?hl=en. >>> >> >> Met vriendelijke groeten, >> >> Jonas Geiregat >> jo...@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-us...@googlegroups.com. >> To unsubscribe from this group, send email to >> django-users+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/django-users?hl=en. >> > > Met vriendelijke groeten, > > Jonas Geiregat > jo...@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-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > Met vriendelijke groeten, Jonas Geiregat jo...@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-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.