From 0a68fe725d3191e20c38003f09924a12148370c6 Mon Sep 17 00:00:00 2001 From: Tobias Bengfort Date: Tue, 6 Apr 2021 12:54:22 +0200 Subject: [PATCH 1/2] configure nplusone --- castellum/settings/development.py | 14 ++++++++++++++ setup.cfg | 1 + 2 files changed, 15 insertions(+) diff --git a/castellum/settings/development.py b/castellum/settings/development.py index d19c257b0..12a5742a1 100644 --- a/castellum/settings/development.py +++ b/castellum/settings/development.py @@ -25,6 +25,20 @@ try: except ImportError: pass +try: + import nplusone + + INSTALLED_APPS += [ + 'nplusone.ext.django', + ] + + MIDDLEWARE = [ + 'nplusone.ext.django.NPlusOneMiddleware', + ] + MIDDLEWARE + + NPLUSONE_RAISE = True +except ImportError: + pass # Database diff --git a/setup.cfg b/setup.cfg index 71e8b72fe..dd4fac4ae 100644 --- a/setup.cfg +++ b/setup.cfg @@ -42,6 +42,7 @@ test = isort pospell docutils == 0.16 + nplusone dev = ipdb django-debug-toolbar -- GitLab From d7df2a2713ac0850ff7f90203ea0911898c0657c Mon Sep 17 00:00:00 2001 From: Tobias Bengfort Date: Tue, 6 Apr 2021 16:36:39 +0200 Subject: [PATCH 2/2] tweak select_related --- castellum/appointments/mixins.py | 12 ++++++++++-- castellum/recruitment/views.py | 4 ++-- castellum/subjects/views.py | 8 ++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/castellum/appointments/mixins.py b/castellum/appointments/mixins.py index 5fa04a942..8850fe11e 100644 --- a/castellum/appointments/mixins.py +++ b/castellum/appointments/mixins.py @@ -77,7 +77,11 @@ class BaseCalendarView(DetailView): template_name = 'appointments/calendar.html' def get_appointments(self): - return Appointment.objects.filter(participation__status=Participation.INVITED) + return ( + Appointment.objects + .filter(participation__status=Participation.INVITED) + .select_related('session', 'participation') + ) def render_appointment(self, appointment): return { @@ -98,7 +102,11 @@ class BaseCalendarView(DetailView): class BaseAppointmentFeed(BaseICalFeed): def items(self, obj): - return Appointment.objects.filter(participation__status=Participation.INVITED) + return ( + Appointment.objects + .filter(participation__status=Participation.INVITED) + .select_related('session', 'participation') + ) def item_start_datetime(self, item): return item.start diff --git a/castellum/recruitment/views.py b/castellum/recruitment/views.py index a732d1b53..20ef54fb1 100644 --- a/castellum/recruitment/views.py +++ b/castellum/recruitment/views.py @@ -112,14 +112,14 @@ class RecruitmentView(StudyMixin, PermissionRequiredMixin, ListView): return Participation.objects\ .with_appointment_count()\ - .prefetch_related('subject')\ + .select_related('subject')\ .filter(status__in=self.shown_status, study=self.study)\ .order_by(*order_by) def _get_statistics(self): participations = Participation.objects.filter( study=self.study, status=Participation.INVITED - ).prefetch_related('subject') + ).select_related('subject') desc1 = get_description_by_statistics_rank('primary') if desc1: diff --git a/castellum/subjects/views.py b/castellum/subjects/views.py index 42de79ace..b9bd585a2 100644 --- a/castellum/subjects/views.py +++ b/castellum/subjects/views.py @@ -81,7 +81,11 @@ class SubjectSearchView(LoginRequiredMixin, FormView): for subject in Subject.objects.filter(filter_queries.pk_filter(contacts)): has_privacy_level = user.has_privacy_level(subject.privacy_level) participations = [] - for participation in subject.participation_set.filter(study__status=Study.EXECUTION): + for participation in ( + subject.participation_set + .filter(study__status=Study.EXECUTION) + .select_related('study') + ): can_access_study = user.has_perm('studies.access_study', obj=participation.study) invited = participation.status == Participation.INVITED can_recruit = ( @@ -255,7 +259,7 @@ class SubjectExportView(SubjectMixin, PermissionRequiredMixin, DetailView): context['appointments'] = Appointment.objects.filter( participation__subject=self.object - ) + ).select_related('session') context['notes'] = self.get_notes() -- GitLab