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: for category in Category.objects.all(): category.num_items = Item.objects.filter(categories__tree_path__startswith=category.tree_path).count() if category.num_items > 0: yield category Suggestions? TIA, 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.