From 3c4437643e47edbaca5f75fd293128268fd0360c Mon Sep 17 00:00:00 2001 From: DavidRThrashJr <60800614+DavidRThrashJr@users.noreply.github.com> Date: Sat, 8 Feb 2020 19:02:47 -0500 Subject: [PATCH] Update models: Bill.pay_each() SQL query is faster for summing up weights --- ihatemoney/models.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ihatemoney/models.py b/ihatemoney/models.py index 250f009b..2313b179 100644 --- a/ihatemoney/models.py +++ b/ihatemoney/models.py @@ -376,8 +376,10 @@ class Bill(db.Model): def pay_each(self): """Compute what each share has to pay""" if self.owers: - # FIXME: SQL might do that more efficiently - weights = sum(i.weight for i in self.owers) + weights = (db.session.query(func.sum(Person.weight)) + .join(billowers, Bill) + .filter(Bill.id == self.id))\ + .scalar() return self.amount / weights else: return 0