[Answer]-Count the related objects of a related object

1👍

The previous answer will count the same building multiple times if they have multiple upgrades. Use this instead:

count = Building.objects.filter(town=town).exclude(upgrade=None).count()
👤Renoc

0👍

I found out with the linked documentation that this works for me:

count = Upgrade.objects.filter(building__town=building.town).count()

Leave a comment