Reduce query count

This commit is contained in:
Gabriel Augendre 2022-10-31 14:47:13 +01:00
parent 03eabc4bcb
commit ac3a0b6dd1
2 changed files with 26 additions and 1 deletions

View file

@ -1,4 +1,5 @@
from django.contrib import admin
from django.forms import ModelForm
from character import models
@ -29,6 +30,9 @@ class PathAdmin(admin.ModelAdmin):
("Documentation", {"fields": ["url"]}),
]
def get_queryset(self, request):
return super().get_queryset(request).select_related("profile", "race")
def related_to(self, instance: models.Path) -> str:
category = models.Path.Category(instance.category)
if category == models.Path.Category.PROFILE:
@ -80,6 +84,21 @@ class RaceAdmin(admin.ModelAdmin):
inlines = [RacialCapabilityInline, PathInline]
class CharacterAdminForm(ModelForm):
class Meta:
model = models.Character
exclude = ()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["capabilities"].queryset = models.Capability.objects.select_related(
"path", "path__race", "path__profile"
)
self.fields[
"racial_capability"
].queryset = models.RacialCapability.objects.select_related("race")
@admin.register(models.Character)
class CharacterAdmin(admin.ModelAdmin):
list_display = ["name", "player", "race", "profile", "level"]
@ -152,6 +171,8 @@ class CharacterAdmin(admin.ModelAdmin):
"weapons",
]
form = CharacterAdminForm
@admin.register(models.Weapon)
class WeaponAdmin(admin.ModelAdmin):

View file

@ -9,7 +9,11 @@ from character.models import Character
@login_required
def characters_list(request):
context = {"characters": Character.objects.filter(player=request.user)}
context = {
"characters": Character.objects.filter(player=request.user).select_related(
"race", "profile"
)
}
return render(request, "character/list.html", context)