aboutsummaryrefslogtreecommitdiff
path: root/models.py
diff options
context:
space:
mode:
authorAlban Gruin2017-10-20 13:29:07 +0200
committerAlban Gruin2017-10-22 13:16:02 +0200
commitb7773b892ee68209feb33870496985f78243681a (patch)
treeb2fe976b0999dca2aa052e4deda171e826f96035 /models.py
parent86efcdcd2df04a59eb5f27212d8d41f60f861f6c (diff)
Modification de la requĂȘte qui sĂ©lectionne les groupes pertinents.
Diffstat (limited to 'models.py')
-rw-r--r--models.py21
1 files changed, 9 insertions, 12 deletions
diff --git a/models.py b/models.py
index db9e262..74fab9e 100644
--- a/models.py
+++ b/models.py
@@ -16,7 +16,8 @@
from functools import reduce
from django.db import models
-from django.db.models import Count, Manager, Q
+from django.db.models import Count, Manager, Q, Subquery
+from django.db.models.expressions import OuterRef
from django.db.models.functions import ExtractWeek, ExtractYear
from django.utils import timezone
from django.utils.text import slugify
@@ -69,17 +70,13 @@ class Timetable(SlugModel):
class GroupManager(Manager):
- def get_relevant_children(self, group):
- parent_in = self.get_queryset().filter(parent=group)
- return self.get_queryset().filter(Q(parent=group) | Q(parent__in=parent_in)) \
- .annotate(children_count=Count("children")) \
- .filter(children_count=0, hidden=False) \
- .order_by("name")
-
- def get_relevant_groups(self, *args, **criteria):
- return self.get_queryset().filter(*args, **criteria) \
- .annotate(children_count=Count("children")) \
- .filter(children_count=0, hidden=False)
+ def get_relevant_groups(self, timetable, *args, **criteria):
+ sub = Group.objects.filter(timetable=timetable,mention=OuterRef("mention"),
+ subgroup__startswith=OuterRef("subgroup")) \
+ .order_by().values("mention").annotate(c=Count("*")).values("c")
+ return Group.objects.filter(*args, timetable=timetable, hidden=False, **criteria) \
+ .annotate(nbsub=Subquery(sub, output_field=models.IntegerField())) \
+ .filter(nbsub=1).order_by("name")
class Group(models.Model):