title: Is possible to implement this function with a single django ORM query?
body: <p>I'm making an aggregate report of stores, categories and customers for an ecommerce. Basically, I'm trying to achieve what this function does:</p>
  <pre class="lang-py prettyprint-override"><code>from django.db.models import ExpressionWrapper, DecimalField, F, Sum
  from .models import Order
  
  
  def get_report_data(stores, categories, customers):
      result = []
      for store in stores:
          for category in categories:
              for customer in customers:
                  total = Order.objects.filter(
                      store=store,
                      orderitem__product__category=category,
                      customer=customer,
                  ).aggregate(
                      total=Sum(
                          ExpressionWrapper(
                              F(&quot;orderitem__product__price&quot;) * F(&quot;orderitem__quantity&quot;),
                              output_field=DecimalField(),
                          )
                      )
                  )[&quot;total&quot;]
                  result.append([store, customer, category, total])
          return result
  </code></pre>
  <p>Obviously, this is efficiently poor and I'm trying to turn this function into a query. I've tried the following, but I know I'm missing the mark by A LOT.</p>
  <pre class="lang-py prettyprint-override"><code>total = (
      Order.objects.filter(
          store__in=stores,
          customer__in=customers,
          orderitem__product__category__in=categories,
      )
      .values(&quot;store&quot;, &quot;orderitem__product__category&quot;)
      .annotate(
          total=Sum(
              ExpressionWrapper(
                  F(&quot;orderitem__product__price&quot;) * F(&quot;orderitem__quantity&quot;),
                  output_field=DecimalField(),
              )
          )
      )
  )
  </code></pre>
  <p>How should I proceed?</p>
  <p>Thank you very much!</p>