1

Have 2 models : MyItem and Category

This works, but how do I do this with a single query using annotate:

for cat in Category.objects.all():
    cat.count = MyItem.objects.filter(category=cat).count()
    cat.save()

Like some:

Category.objects.annotate(_items_count=Count('myitem')).update(count=_items_count)
Roman Nozhenko
  • 698
  • 8
  • 21

0 Answers0