aboutsummaryrefslogtreecommitdiff
path: root/models.py
diff options
context:
space:
mode:
authorAlban Gruin2018-01-20 22:28:21 +0100
committerAlban Gruin2018-01-20 22:45:00 +0100
commita0101b9a7566b5296c3490ce3592984976553807 (patch)
tree3d494c6279a02ba5e8159d93cbb61e88aeda3ddb /models.py
parent046e92137ace30cd645ba0f42421c283a60ba0cd (diff)
On cache les groupes qui n’ont plus de cours
La requête est assez longue à s’effectuer sur SQLite, mais pas sur PostgreSQL
Diffstat (limited to 'models.py')
-rw-r--r--models.py9
1 files changed, 8 insertions, 1 deletions
diff --git a/models.py b/models.py
index 15f378a..c122bfd 100644
--- a/models.py
+++ b/models.py
@@ -16,7 +16,7 @@
from functools import reduce
from django.db import models
-from django.db.models import Manager, Q
+from django.db.models import Count, Manager, OuterRef, Q, Subquery
from django.db.models.functions import ExtractWeek, ExtractYear
from django.utils import timezone
from django.utils.text import slugify
@@ -104,6 +104,13 @@ class GroupManager(Manager):
mention=group.mention,
source=group.source)
+ def get_relevant_groups(self, start, **criteria):
+ courses = Course.objects.filter(groups=OuterRef("pk"), begin__gte=start) \
+ .only("pk")[:1]
+ return self.get_queryset().annotate(c=Subquery(courses,
+ output_field=models.IntegerField())) \
+ .filter(c__isnull=False, **criteria).order_by("name")
+
class Group(SlugModel):
objects = GroupManager()