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’