Django ORM Group By through two fields and sum result into third

Viewed 31

I have a Move model and those objects

#python
from django.db import models
from django.utils.translation import gettext_lazy as _

class Move(modesl.Model):
    shipment_from = models.CharField(
        max_length=10,
        verbose_name=_("From"),
    )
    shipment_to = models.CharField(
        max_length=10,
        verbose_name=_("To"),
    )

And I can group by shipment_from and group by shipment_to. Result of two queries

The question is: how do I achieve this result where total summarize for place with the same title?

# json
[
   {
      "place":"A",
      "total":3
   },
   {
      "place":"B",
      "total":3
   },
   {
      "place":"C",
      "total":2
   },
   {
      "place":"D",
      "total":4
   },
   {
      "place":"E",
      "total":2
   },
   {
      "place":"G",
      "total":1
   }
]
0 Answers
Related