# sum a grouped data

**URL:** <https://forum.djangoproject.com/t/sum-a-grouped-data/13915>\
**Category:** Using the ORM\
**Created:** [May 25, 2022, 1:10pm UTC](https://forum.djangoproject.com/t/sum-a-grouped-data/13915 "2022-05-25T13:10:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhrnhr0](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nhrnhr0/32/1613_2.png) [@nhrnhr0](https://forum.djangoproject.com/u/nhrnhr0)\
**Post date:** [May 25, 2022, 1:10pm UTC](https://forum.djangoproject.com/t/sum-a-grouped-data/13915/1 "2022-05-25T13:10:40Z")

</div>

Hey, I have a modal to represent the inventory in my retail store

```auto
class WarehouseStock(models.Model):
    warehouse = models.ForeignKey(to='Warehouse', on_delete=models.CASCADE)
    product = models.ForeignKey(to=Product, on_delete=models.CASCADE)
    size = models.ForeignKey(to=ProductSize, on_delete=models.SET_NULL, null=True, blank=True)
    color = models.ForeignKey(to=Color, on_delete=models.SET_NULL, null=True, blank=True)
    quantity = models.IntegerField(default=0)

```

When I get a request from API I want to send the available inventory of the product.  
the API doesn’t care in what warehouses the product is stored, he only wants the total quantity from every size and color

I tried to do something like this:

```auto
WarehouseStock.objects.filter(product=product).values('size', 'color').annotate(total=Sum('quantity'))

```

the given result:

```auto
{'size': 86, 'color': 76, 'total': 3}, // warehouse 1
{'size': 86, 'color': 76, 'total': 4}, // warehouse 2 - same as above
{'size': 86, 'color': 77, 'total': 5}, // warehouse 1
{'size': 80, 'color': 76,'total': 5} // warehouse 2

```

wanted result:

```auto
{'size': 86, 'color': 76, 'total': 7}, // warehouse 1 and warehouse 2 
{'size': 86, 'color': 77, 'total': 5}, // warehouse 1
{'size': 80, 'color': 76,'total': 5} // warehouse 2

```

How would I do it?  
I hope I’m clear. Thank you very much

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 25, 2022, 2:29pm UTC](https://forum.djangoproject.com/t/sum-a-grouped-data/13915/2 "2022-05-25T14:29:24Z")

</div>

> [@nhrnhr0](#):
>
> `WarehouseStock.objects.filter(product=product).values('size', 'color').annotate(total=Sum('quantity'))`

Try adding the `order_by('size', 'color')` clause after the `values` clause. e.g.:  
`WarehouseStock.objects.filter(product=product).values('size', 'color').order_by('size', 'color').annotate(total=Sum('quantity'))`

---

<div class="post-metadata">

**Author:** ![nhrnhr0](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nhrnhr0/32/1613_2.png) [@nhrnhr0](https://forum.djangoproject.com/u/nhrnhr0)\
**Post date:** [May 25, 2022, 3:32pm UTC](https://forum.djangoproject.com/t/sum-a-grouped-data/13915/3 "2022-05-25T15:32:56Z")

</div>

Thank you!  
I’m really confused about why this worked  
But it did!  
Thank you

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 25, 2022, 5:14pm UTC](https://forum.djangoproject.com/t/sum-a-grouped-data/13915/4 "2022-05-25T17:14:33Z")

</div>

It’s covered in the docs in this section: [Aggregation | Django documentation | Django](https://docs.djangoproject.com/en/4.0/topics/db/aggregation/#values)
