I have problem with query lazy execution in my custom Manager method. In it i want to separate query by model CharField choices and return dict[choice, QuerySet].
model.py part:
...
PRODUCT_STATUS = [
('pn', 'Запланировано'),
('ga', 'В процессе (Ознакомляюсь)'),
('rv', 'Повтор'),
('ac', 'Завершено'),
('ab', 'Брошено'),
('pp', 'Отложено'),
]
class ExtendedManager(models.Manager):
def separated_by_status(self, product_type):
query = super().get_queryset().all()
dict_ = {}
for status in PRODUCT_STATUS:
dict_.update({status[1]: query.filter(status=status[0]).all()})
return dict_
...
views.py part with manager use:
...
class ProductListView(ContextMixin, View):
template_name = 'myList/myList.html'
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
product = kwargs.get('product')
if product not in {'film', 'game', 'series', 'book'}:
raise Http404
context = self.get_context_data()
context['title'] = f'Список {product}'
context['dict_queryset'] = Product.objects.separated_by_status(product)
return render(request, self.template_name, context)
...
django debug toolbar result: here
The problem is that the query is lazy executed in the manager, but in templates it is already fully executed for each PRODUCT_STATUS element separately. How cat it be optimized to execute 1 time?
I'm very sorry if I use the term "lazy" incorrectly.