From de0b1668e5f5475e25e7613a959d40b1589ab837 Mon Sep 17 00:00:00 2001 From: Daniel Prange Date: Tue, 28 May 2024 11:12:15 +0300 Subject: [PATCH] perf(hearing): optimize open filter Replace union & distinct with or statements. Refs KER-366 --- democracy/views/hearing.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/democracy/views/hearing.py b/democracy/views/hearing.py index 94b79cc8..28826bc9 100644 --- a/democracy/views/hearing.py +++ b/democracy/views/hearing.py @@ -2,7 +2,7 @@ from collections import defaultdict from django.conf import settings from django.db import transaction -from django.db.models import Prefetch +from django.db.models import Prefetch, Q from django.utils import timezone from rest_framework import filters, permissions, response, serializers, status, viewsets from rest_framework.decorators import action @@ -68,11 +68,9 @@ def filter_open(self, queryset, name, value): .filter(force_closed=False) ) else: - return ( - queryset.filter(close_at__lte=timezone.now()) - | queryset.filter(open_at__gt=timezone.now()) - | queryset.filter(force_closed=True) - ).distinct() + return queryset.filter( + Q(close_at__lte=timezone.now()) | Q(open_at__gt=timezone.now()) | Q(force_closed=True) + ) def filter_created_by(self, queryset, name, value): if not self.request.user: