Browse Source

Use aggregation to compute the end date of membership (more efficient)

Baptiste Jonglez 10 years ago
parent
commit
464de9c936
1 changed files with 3 additions and 4 deletions
  1. 3 4
      coin/members/models.py

+ 3 - 4
coin/members/models.py

@@ -5,7 +5,7 @@ import ldapdb.models
 import unicodedata
 import datetime
 from django.db import models
-from django.db.models import Q
+from django.db.models import Q, Max
 from django.db.models.signals import pre_save
 from django.dispatch import receiver
 from django.contrib.auth.models import AbstractUser
@@ -107,9 +107,8 @@ class Member(CoinLdapSyncMixin, AbstractUser):
 
     # Renvoie la date de fin de la dernière cotisation du membre
     def end_date_of_membership(self):
-        x = self.membership_fees.order_by('-end_date')
-        if x:
-            return self.membership_fees.order_by('-end_date')[0].end_date
+        aggregate = self.membership_fees.aggregate(end=Max('end_date'))
+        return aggregate['end']
     end_date_of_membership.short_description = "Date de fin d'adhésion"
 
     def is_paid_up(self, date=None):