Commit 9064d997 authored by Tom van Bussel's avatar Tom van Bussel

Optimize the activemembers admin

This commit makes the activemembers admin a lot faster by doing the
following two things:

1. Add `member` to `raw_id_fields` in the inline for committee
   memberships. Before the entire list of members was copied for every
   committee membership. This resulted in html pages of several
   megabytes.
2. Remove duplicate queries (for different ids) by adding
   `select_related` calls in the right places.
parent 7e908c53
from django.contrib import admin
from django.contrib.auth.models import Permission
from django.forms import BaseInlineFormSet, ModelForm
from activemembers.forms import CommitteeMembershipForm
from utils.translation import TranslatedModelAdmin
from . import models
class CommitteeMembershipInlineFormSet(BaseInlineFormSet):
"""
Solely here for performance reasons.
Needed because the `__str__()` of `CommitteeMembership` (which is displayed
above each inline form) uses the username, name of the member and name of
the committee.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.queryset = self.queryset.select_related('member', 'committee')
class CommitteeMembershipInline(admin.StackedInline):
model = models.CommitteeMembership
formset = CommitteeMembershipInlineFormSet
can_delete = False
ordering = ('since',)
extra = 0
# TODO: replace this with `autocomplete_fields` in Django 2.0
raw_id_fields = ('member',)
class CommitteeForm(ModelForm):
"""
Solely here for performance reasons.
Needed because the `__str__()` of `Permission` (which is displayed in the
permissions selection box) also prints the corresponding app and
`content_type` for each permission.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['permissions'].queryset = (Permission
.objects
.select_related('content_type'))
@admin.register(models.Committee)
class CommitteeAdmin(TranslatedModelAdmin):
inlines = (CommitteeMembershipInline,)
form = CommitteeForm
list_display = ('name', 'since', 'until', 'active', 'email')
list_filter = ('until', 'active',)
search_fields = ('name', 'description')
......@@ -39,6 +75,7 @@ class CommitteeAdmin(TranslatedModelAdmin):
@admin.register(models.Board)
class BoardAdmin(TranslatedModelAdmin):
inlines = (CommitteeMembershipInline,)
form = CommitteeForm
exclude = ('is_board',)
filter_horizontal = ('permissions',)
......@@ -51,10 +88,11 @@ class CommitteeMembershipAdmin(TranslatedModelAdmin):
form = CommitteeMembershipForm
list_display = ('member', 'committee', 'since', 'until', 'chair', 'role')
list_filter = ('committee',)
list_select_related = ('member', 'committee',)
search_fields = ('member__first_name', 'member__last_name',
'member__email')
@admin.register(models.Mentorship)
class MentorsAdmin(admin.ModelAdmin):
pass
class MentorshipAdmin(admin.ModelAdmin):
list_select_related = ('member',)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment