开发者

Django: counting M2MField of an M2MField?

开发者 https://www.devze.com 2023-02-03 09:49 出处:网络
I have models as follows: class Place(models.Model): name = models.CharField(max_length=300) class Person(models.Model):

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()])
0

精彩评论

暂无评论...
验证码 换一张
取 消