From fe085664d5e42f1d9bd47f6425ddbdfe9fe34f9e Mon Sep 17 00:00:00 2001 From: Pablo Moreno Date: Wed, 19 Feb 2025 19:26:28 +0100 Subject: [PATCH] feat: added prefetch related for product prices --- web/templates/components/product_card.html | 4 +++- web/views/components.py | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/web/templates/components/product_card.html b/web/templates/components/product_card.html index 19a5f54..9ccd27f 100644 --- a/web/templates/components/product_card.html +++ b/web/templates/components/product_card.html @@ -29,7 +29,9 @@

- {{ product.price.price_with_tax }}€ + {% with product.current_prices.0 as price %} + {{ price.price_with_tax }} € + {% endwith %}

diff --git a/web/views/components.py b/web/views/components.py index d508935..032228d 100644 --- a/web/views/components.py +++ b/web/views/components.py @@ -1,6 +1,7 @@ from decimal import Decimal from django.db import models +from django.db.models import Prefetch from django.http import HttpResponse from django.shortcuts import get_object_or_404, render from django.urls import reverse @@ -29,6 +30,7 @@ class ListProducts(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin): template_name = "web/list_products.html" queryset = Product.objects.filter(hidden=False).prefetch_related( "images", + Prefetch("prices", queryset=ProductPrice.objects.filter(current=True), to_attr="current_prices") ) filter_class = ProductFilter