django aggregate sum?

Tell me how to get the quantity of products at each price. The same product, but at different prices.
For example,
Product 1, price =5, quantity =10, id_prod=1
Product 1, price =5, quantity =20, id_prod=1
Product 1, price =15, quantity =10, id_prod=1
Product 2, price =5, quantity =10, id_prod=2
Product 1, price =25,7, quantity =100, id_prod=1
Product 2, price =57, quantity =10, id_prod=2
Product 1, price =25,7, quantity =100, id_prod=1

In the end I want to get the quantity for each item :

Product 1, price =5, total_quantity =30, id_prod=1
Product 1, price =15, total_quantity =10, id_prod=1
Product 2, price =5, total_quantity =10, id_prod=1
Product 1, price =25,7, total_quantity =200, id_prod=1

I thought to use a cycle to get a unique the value of the Product + Price and sum up the quantity. Maybe there is an easier way?

Group by id and price?

Something like:

model.objects.all()….annotate(Count('price','id_prod'))

Not sure that would work…

I’m not understanding how you’re obtaining those results from the data you’ve provided.

Specifically, this result -

I don’t see how you get this from:

[emphasis added]

Was this just a typo?

Assuming that that is a typo, you’re looking for something like:
Product.objects.values('name', 'id_prod', 'price').annotate(total=Sum('quantity'))

It’s just a typo, of course

 uniqidset=set(JurnalDoc.objects.values_list('title__title','price'))
    for i in uniqidset:
        print(i)

in console:
(‘Товар 11’, 3.0)
(‘Товар1’, 3.0)
(‘Товар10’, 24.0)
(‘Товар 11’, 21.0)
(‘Товар 11’, 18.0)
Like, right…
And then it doesn’t work like that

 uniqidset=set(JurnalDoc.objects.values_list('title__title','price').annotate(total=Sum('kol')))
    for i in uniqidset:
        print(i)

(‘Товар10’, 24.0, 12.0)
(‘Товар 11’, 18.0, 2.0)
(‘Товар1’, 3.0, 4.0)
(‘Товар 11’, 3.0, 12.0)
(‘Товар 11’, 3.0, 50.0)
(‘Товар 11’, 21.0, 12.0)
And I would like to get something similar to this
(‘Товар10’, 24.0, 12.0)
(‘Товар 11’, 18.0, 2.0)
(‘Товар1’, 3.0, 4.0)
(‘Товар 11’, 3.0, 62.0)
(‘Товар 11’, 21.0, 12.0)
Could you please tell me how this can be done?
Thanks!

Please post your complete model, and if you are using a custom model manager, post it as well.

Also, did you try what I suggested at django aggregate sum? - #3 by KenWhitesell

class Nom(models.Model):
    title = models.CharField(max_length=150, verbose_name='Наименование', unique=True )
    izm=models.ForeignKey(Unit,verbose_name='Ед.изм.',on_delete=models.PROTECT)
    category = models.ForeignKey(Category, verbose_name='Категория', on_delete=models.PROTECT)
    srok=models.IntegerField(blank=True,default=0)
    updated_at = models.DateTimeField(auto_now=True, verbose_name='Создан')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='Обновлен')
    def __str__(self):
        return self.title
class JurnalDoc(models.Model):
 
    oper=models.IntegerField(max_length=1,verbose_name='Операция')
    iddoc=models.ForeignKey(Jurnal,on_delete=models.PROTECT)
    title=models.ForeignKey(Nom,verbose_name='Наименование',on_delete=models.PROTECT)
    price=models.FloatField(verbose_name='Цена',default=0.0)
    kol = models.FloatField(verbose_name='Количество', default=0.0)
    podraz=models.ForeignKey(Podraz,on_delete=models.PROTECT,verbose_name='Подразделение')
    postav=models.ForeignKey(Postav,on_delete=models.PROTECT,verbose_name='Поставщик')
    obct=models.ForeignKey(Obct,on_delete=models.PROTECT,verbose_name='Объект')
    fio=models.ForeignKey(Fio,on_delete=models.PROTECT,verbose_name='Подотчетник')
    spis=models.ForeignKey(Spis,on_delete=models.PROTECT,null=True,blank=True,verbose_name='Причина списания')
    summa=models.FloatField(verbose_name='Сумма')
    nds=models.IntegerField(default=20,verbose_name='НДС')
    summawithnds=models.FloatField(verbose_name='Сумма с НДС')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='Создан')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='Обновлен')
    uniqfield=models.CharField(max_length=250,verbose_name='Слаг')
    def __str__(self):
        return self.title

Well, title is probably an unfortunate field name… id_nomenklatura or id_nom would be more logical… But that’s what I call it…

Here I get a set, everything seems to be as it should be…

uniqidset=set(JurnalDoc.objects.values_list('title__title','price'))
    for i in uniqidset:
        print(i)

I output to the console: that’s right, there are no duplicate values …
(‘Товар2’, 50.0)
(‘Товар1’, 200.0)
(‘Товар 3’, 100.0)
(‘Товар1’, 100.0)
Adding annotate:

uniqidset=set(JurnalDoc.objects.values_list('title__title','price').annotate(count=Sum('kol')))
    for i in uniqidset:
        print(i)

Oops!
(‘Товар2’, 50.0, 30.0)
(‘Товар 3’, 100.0, 45.0)
(‘Товар1’, 100.0, 20.0)
(‘Товар2’, 50.0, 20.0)
(‘Товар1’, 200.0, 20.0)

And I would like to get:

(‘Товар2’, 50.0, 50.0)
(‘Товар 3’, 100.0, 45.0)
(‘Товар1’, 100.0, 20.0)
(‘Товар1’, 200.0, 20.0)

I tried it like this:

uniqidset=set(JurnalDoc.objects.values('title__title','price').annotate(count=Sum('kol')))

And I get an error

TypeError at /GetActualData/

unhashable type: ‘dict’

Yes!!!
uniqidset=JurnalDoc.objects.values(‘title’,‘price’).annotate(count=Sum(‘kol’)).order_by(‘title’,‘price’)
{‘title__title’: ‘Товар 3’, ‘price’: 100.0, ‘count’: 45.0}
{‘title__title’: ‘Товар1’, ‘price’: 100.0, ‘count’: 40.0}
{‘title__title’: ‘Товар1’, ‘price’: 200.0, ‘count’: 20.0}
{‘title__title’: ‘Товар2’, ‘price’: 50.0, ‘count’: 50.0}