On Tue, Nov 2, 2010 at 9:35 AM, Itai Tavor <[email protected]> wrote:
> Hi people,
>
> Given this:
>
> class Category(models.Model):
> name = models.CharField()
> parent = models.ForeignKey('self', blank=True, null=True)
> tree_path = models.CharField() # /<root_id>/.../<parent_id>/<id>/
>
> class Item(models.Model):
> name = models.CharField()
> categories = models.ManyToManyField(Category)
>
> I need a Category queryset, with each category annotated with a count of
> items which have categories whose tree_path starts with the tree_path of the
> current category, and with categories for which this count is 0 filtered
> out.
>
> In case that wasn't clear - I need an efficient way to do this:
>
>
You probably need to use annotate
http://docs.djangoproject.com/en/dev/ref/models/querysets/#annotate-args-kwargs
--
regards,
Prashanth
twitter: munichlinux
blog: honeycode.in
irc: munichlinux, JSLint, munichpython.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.