From ea6f7d2849e08fe164ce2532e5485695b1c0b675 Mon Sep 17 00:00:00 2001 From: Luca Date: Tue, 10 May 2022 16:33:12 +0200 Subject: [PATCH] Fix 'Next shifts' when there are 'empty' rooms --- shiftregister/team/views.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/shiftregister/team/views.py b/shiftregister/team/views.py index 5fe9667..4cfc679 100644 --- a/shiftregister/team/views.py +++ b/shiftregister/team/views.py @@ -29,13 +29,16 @@ def shift_overview(request): .order_by("start_at") ] - # probably can do some distinct/group by stuff but not sure how tih django queries - context["next_shifts"] = [ - Shift.objects.filter(room=room, start_at__gt=timezone.now(), deleted=False) - .order_by("start_at") - .first() - for room in Room.objects.all() - ] + # only Postgres supports DISTINCT on specific columns, SQLite does not support aggregates on datetime fields + context["next_shifts"] = filter( + lambda x: x is not None, + ( + Shift.objects.filter(room=room, start_at__gt=timezone.now(), deleted=False) + .order_by("start_at") + .first() + for room in Room.objects.all() + ), + ) return render(request, "shift_overview.html", context)