开发者

Django, sum of fields in intermediary model with given queryset

开发者 https://www.devze.com 2023-03-28 19:51 出处:网络
I have 2 models, one of them has many to many relation with itself through other table like this. class a(models.Model):

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.


from django.db.models import Sum
b.objects.filter(from_a__whatever='something').aggregate(Sum('count'))
0

精彩评论

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

关注公众号