tenants: optimise db queries in middleware

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-09-13 17:54:37 +02:00
parent 8e59b06611
commit fb267ee223
1 changed files with 3 additions and 2 deletions

View File

@ -20,9 +20,10 @@ def get_tenant_for_request(request: HttpRequest) -> Tenant:
.filter(Q(host_domain__iendswith=F("domain")) | _q_default) .filter(Q(host_domain__iendswith=F("domain")) | _q_default)
.order_by("default") .order_by("default")
) )
if not db_tenants.exists(): tenants = list(db_tenants.all())
if len(tenants) < 1:
return DEFAULT_TENANT return DEFAULT_TENANT
return db_tenants.first() return tenants[0]
def context_processor(request: HttpRequest) -> dict[str, Any]: def context_processor(request: HttpRequest) -> dict[str, Any]: