mirror of
https://github.com/Crocmagnon/charasheet.git
synced 2024-11-22 14:38:03 +01:00
Reduce query count
This commit is contained in:
parent
03eabc4bcb
commit
ac3a0b6dd1
2 changed files with 26 additions and 1 deletions
|
@ -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):
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue