diff options
Diffstat (limited to 'models.py')
-rw-r--r-- | models.py | 13 |
1 files changed, 11 insertions, 2 deletions
@@ -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 @@ -31,6 +31,7 @@ class SlugModel(models.Model): super(SlugModel, self).save(*args, **kwargs) + class Meta: abstract = True @@ -104,6 +105,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() @@ -213,7 +221,8 @@ class CourseManager(Manager): .annotate(year=ExtractYear("begin"), week=ExtractWeek("begin")) \ .values("groups__mention", "groups__semester", - "groups__subgroup", "year", "week") + "groups__subgroup", "year", "week") \ + .annotate(c=Count("*")) class Course(models.Model): |