hey,
my admin page takes a long time to load, I started to work with debug_toolbar
and I have some questions.
this is what I see when I open my product
model:
103 queries it’s too much!
my admin:
class ProductAdmin(admin.ModelAdmin): # in product/admin.py
list_display = (..., 'total_amount')
readonly_fields(...,'total_amount')
def get_queryset(self, request):
qs = Product.objects.select_related('category').prefetch_related('images','stocks')
return qs
class Stock(models.Model): # in stock/moduls.py
class Meta():
verbose_name = _('Stock')
verbose_name_plural = _('Stocks')
default_related_name = 'stocks'
def total_amount(self, *args, **kwargs): # in product/moduls.py - class Product(models.Model)
from stock.models import Stock
from django.db.models import Sum
#stocks = Stock.objects.filter(product=self)
stocks = self.stocks
res = stocks.aggregate(Sum('amount'))
return res['amount__sum']
total_amount.short_description = _("total stock at us")
- why does I still do a query to the database for every stock to aggregate the sum after I prefetched it?
I can that I have 91 similar queries - should I call
get_queryset
super? if so, in what way and why? - what
queryset
I get in theget_search_results
?
I would love to hear any other tips on optimization as well
Thank you!