From 36d658e57f6ad56c31604d5acfdeaf4d1ea4c968 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Wed, 5 Oct 2016 12:52:56 -0700 Subject: [PATCH] More query optimisation --- registrasion/reporting/views.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/registrasion/reporting/views.py b/registrasion/reporting/views.py index 7a74728d..28dfa037 100644 --- a/registrasion/reporting/views.py +++ b/registrasion/reporting/views.py @@ -467,7 +467,7 @@ def attendee(request, form, user_id=None): payments = commerce.PaymentBase.objects.filter( invoice__user=attendee.user, ).select_related("invoice") - + reports.append(QuerysetReport( "Payments", ["invoice__id", "id", "reference", "amount"], @@ -481,11 +481,18 @@ def attendee(request, form, user_id=None): def attendee_list(request): ''' Returns a list of all attendees. ''' - attendees = people.Attendee.objects.all().select_related( + attendees = people.Attendee.objects.select_related( "attendeeprofilebase", "user", ) + profiles = AttendeeProfile.objects.filter( + attendee__in=attendees + ).select_related( + "attendee", "attendee__user", + ) + profiles_by_attendee = dict((i.attendee, i) for i in profiles) + attendees = attendees.annotate( has_registered=Count( Q(user__invoice__status=commerce.Invoice.STATUS_PAID) @@ -501,8 +508,8 @@ def attendee_list(request): for a in attendees: data.append([ a.user.id, - a.attendeeprofilebase.attendee_name() - if hasattr(a, "attendeeprofilebase") else "", + (profiles_by_attendee[a].attendee_name() + if a in profiles_by_attendee else ""), a.user.email, a.has_registered > 0, ])