On remplace prefetch_related par select_related là où c'est possible

This commit is contained in:
Jean-Marie Favreau 2024-11-23 10:52:59 +01:00
parent ecc347219c
commit 5a66caae55
3 changed files with 5 additions and 5 deletions

View File

@ -242,7 +242,7 @@ class CalendarList:
Q(other_versions__isnull=True) |
Q(other_versions__representative=F('pk')) |
Q(other_versions__representative__isnull=True)
).order_by("start_time", "title__unaccent__lower").prefetch_related("exact_location").prefetch_related("category").prefetch_related("other_versions").prefetch_related("other_versions__representative")
).order_by("start_time", "title__unaccent__lower").select_related("exact_location").select_related("category").select_related("other_versions").select_related("other_versions__representative")
firstdate = datetime.fromordinal(self.c_firstdate.toordinal())
if firstdate.tzinfo is None or firstdate.tzinfo.utcoffset(firstdate) is None:

View File

@ -1978,7 +1978,7 @@ class CategorisationRule(models.Model):
def get_category_from_rules(event):
cats = defaultdict(lambda: 0)
if CategorisationRule.rules is None:
CategorisationRule.rules = CategorisationRule.objects.all().prefetch_related("category").prefetch_related("place")
CategorisationRule.rules = CategorisationRule.objects.all().select_related("category").select_related("place")
for rule in CategorisationRule.rules:
if rule.match(event):

View File

@ -1492,7 +1492,7 @@ def set_duplicate(request, year, month, day, pk):
@login_required(login_url="/accounts/login/")
@permission_required("agenda_culturel.view_categorisationrule")
def categorisation_rules(request):
paginator = Paginator(CategorisationRule.objects.all().order_by("pk").prefetch_related("category").prefetch_related("place"), 100)
paginator = Paginator(CategorisationRule.objects.all().order_by("pk").select_related("category").select_related("place"), 100)
page = request.GET.get("page")
try:
@ -1584,7 +1584,7 @@ def apply_categorisation_rules(request):
else:
# first we check if events are not correctly categorised
to_categorise = []
events = Event.objects.filter(start_day__gte=datetime.now()).exclude(category=Category.get_default_category_id()).exclude(category=None).prefetch_related("exact_location").prefetch_related("category")
events = Event.objects.filter(start_day__gte=datetime.now()).exclude(category=Category.get_default_category_id()).exclude(category=None).select_related("exact_location").select_related("category")
for e in events:
c = CategorisationRule.get_category_from_rules(e)
if c and c != e.category:
@ -1593,7 +1593,7 @@ def apply_categorisation_rules(request):
# then we apply rules on events without category
nb = 0
to_save = []
events = Event.objects.filter(start_day__gte=datetime.now()).filter(Q(category=Category.get_default_category_id()) | Q(category=None)).prefetch_related("exact_location").prefetch_related("category")
events = Event.objects.filter(start_day__gte=datetime.now()).filter(Q(category=Category.get_default_category_id()) | Q(category=None)).select_related("exact_location").select_related("category")
for e in events:
success = CategorisationRule.apply_rules(e)
if success: