[Answered ]-Django: how to make query be not lazy executed?

1👍

The problem is not the laziness, the problem is that you make a QuerySet per PRODUCT_STATUS.

You can make such dictionary with a single pass with the groupby function [Python-doc] of the itertools module [Python-doc]:

from itertools import groupby
from operator import attrgetter

class ExtendedManager(models.Manager):
    def separated_by_status(self, product_type):
        query = super().get_queryset().order_by('status')
        dict_ = {
            k: list(vs)
            for k, vs in groupby(query, attrgetter('status'))
        }
        return {
            status1: dict_.get(status0, [])
            for status0, status1 in PRODUCT_STATUS
        }

Leave a comment