Django: counting M2MField of an M2MField?
I have models as follows:
class Place(models.Model):
name = models.CharField(max_length=300)
class Person(models.Model):
name = models.CharField(max_length=300)
class Manor(models.Model):
place = models.ManyToManyField(Place, related_name="place"))
lord = models.ManyToManyField(Person, related_name="lord")
ov开发者_Python百科erlord = models.ManyToManyField(Person, related_name="overlord")
If I do {{ person.lord.count }}
in my template, I can get the count of the Manors attached to each Person.
Is there a way I can do an equivalent of {{ person.lord.place.count }}
(which doesn't work), to get the count of the Places attached to the Manors attached to each Person?
Thanks!
Try this:
sum([lord.place.count() for lord in person.lords.all()])
精彩评论