On 03/11/2010, at 2:10 AM, Prashanth wrote:
> 
> 
> On Tue, Nov 2, 2010 at 9:35 AM, Itai Tavor <itai.ta...@gmail.com> 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

Thanks, but I don't think it this particular query can be done just with django 
annotations. If it can, I don't know how. But I think it would require either 
an extra select or raw SQL.

Itai

-- 
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.

Reply via email to