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()
0👍
I found out with the linked documentation that this works for me:
count = Upgrade.objects.filter(building__town=building.town).count()
Source:stackexchange.com