From d619331f62b28fd69d3f998d97e4343dd0ed6bc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matja=C5=BE=20Horvat?= Date: Thu, 10 Oct 2024 17:24:29 +0200 Subject: [PATCH] Reduce the number of DB queries when retrieving translation history (#3404) --- pontoon/base/views.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pontoon/base/views.py b/pontoon/base/views.py index 03d43035e5..ec0ee28f90 100755 --- a/pontoon/base/views.py +++ b/pontoon/base/views.py @@ -13,7 +13,7 @@ from django.contrib.auth.models import User from django.core.paginator import EmptyPage, Paginator from django.db import transaction -from django.db.models import Q +from django.db.models import Prefetch, Q from django.http import ( Http404, HttpResponse, @@ -403,7 +403,17 @@ def get_translation_history(request): translations = Translation.objects.filter( entity=entity, locale=locale, - ).prefetch_related("comments") + ).prefetch_related( + Prefetch( + "comments", + queryset=Comment.objects.prefetch_related("author").order_by("timestamp"), + ), + "user", + "approved_user", + "rejected_user", + "errors", + "warnings", + ) if plural_form != -1: translations = translations.filter(plural_form=plural_form) @@ -425,7 +435,7 @@ def get_translation_history(request): "approved_date": t.approved_date, "rejected_user": User.display_name_or_blank(t.rejected_user), "rejected_date": t.rejected_date, - "comments": [c.serialize() for c in t.comments.order_by("timestamp")], + "comments": [c.serialize() for c in t.comments.all()], "machinery_sources": t.machinery_sources_values, } )