Reputation: 13748
I have 2 models, one of them has many to many relation with itself through other table like this.
class a(models.Model):
# fields
class b(models.Model):
from_a = models.ForeignKey(a)
to_a = models.ForeignKey(a)
count = models.PositiveIntegerField()
Now, what I wonder is, what is the best way of calculating sum of counts in b's where from_a is "something". This one seems trivial, but I can't figure it out.
Upvotes: 1
Views: 360
Reputation: 599620
from django.db.models import Sum
b.objects.filter(from_a__whatever='something').aggregate(Sum('count'))
Upvotes: 3