2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2026-02-08 13:18:15 +00:00

First attempts at better use of Django query optimisation

This commit is contained in:
2025-01-10 00:28:01 +00:00
parent 486a50f876
commit 49c0c0fe3a
5 changed files with 23 additions and 29 deletions

View File

@@ -16,7 +16,7 @@ from troggle.parsers.imports import import_logbook
"""These views are for logbook items when they appear in an 'expedition' page
and for persons: their individual pages and their perseonexpedition pages.
It uses the global object TROG to hold some cached pages.
It uses the global object TROG to hold some cached pages. USELESS as cache only works single-threaded, single-user.
"""
todo = """- Fix the get_person_chronology() display bug.
@@ -91,17 +91,16 @@ def expedition(request, expeditionname):
# print('! - expo {expeditionanme} using cached page')
return render(request, "expedition.html", {**ts[expeditionname], "logged_in": logged_in})
expeditions = Expedition.objects.all() # top menu only, evaluated only when template renders
entries = expo.logbookentry_set.all()
blocks = expo.survexblock_set.all()
entries = expo.logbookentry_set.only('date','title').filter(expedition=expo)
blocks = expo.survexblock_set.only('date','name').filter(expedition=expo).prefetch_related('scanswallet', 'survexfile')
dateditems = list(entries) + list(blocks) # evaluates the Django query and hits db
dates = sorted(set([item.date for item in dateditems]))
allpersonlogentries = PersonLogEntry.objects.filter(personexpedition__expedition=expo)
allpersonlogentries = PersonLogEntry.objects.prefetch_related('logbook_entry').select_related('personexpedition__expedition').filter(personexpedition__expedition=expo)
personexpodays = []
for personexpedition in expo.personexpedition_set.all():
for personexpedition in expo.personexpedition_set.all().prefetch_related('person'):
expotrips = allpersonlogentries.filter(personexpedition=personexpedition) # lazy
expoblocks = blocks.filter(survexpersonrole__personexpedition=personexpedition)
@@ -118,6 +117,8 @@ def expedition(request, expeditionname):
prow.append(pcell)
personexpodays.append({"personexpedition": personexpedition, "personrow": prow, "sortname": personexpedition.person.last_name})
expeditions = Expedition.objects.only('year') # top menu only, evaluated only when template renders, only need "year"
ts[expeditionname] = {
"year": int(expeditionname),
"expedition": expo,