S
S
Stanislav2020-04-24 19:45:54
Django
Stanislav, 2020-04-24 19:45:54

Django ORM: How to reduce database queries?

I make a request to select the number of products in the filters, but I get a large number of requests to the database. Is it possible to reduce their number to 1?

data = Product.objects.filter(category__in=descendants)

filters = {
    'color': data.values('color__name').filter(q_color).annotate(total=Count('color__name')),
    'style': data.values('style').filter(q_style).annotate(total=Count('style')),
    'tracery': data.values('tracery').filter(q_tracery).annotate(total=Count('tracery')),
    'size': data.values('sizes').filter(sizes__quantity__gt=0).filter(q_size).annotate(total=Count('sizes')),
    'material': data.values('composition__material__name').filter(q_material).annotate(
        total=Count('composition__material__name'))}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
django-jobs, 2020-04-24
@django-jobs

Have you tried caching?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question