sum a grouped data

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

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:

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

the given result:

{'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:

{'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

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'))

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

It’s covered in the docs in this section: Aggregation | Django documentation | Django