i have no expernce with 'aggregation' and 'annotate' and talk with exemple better i have this object :
mediasec = MediaSec.objects.filter(date__range=[primary1, primary2], degraycomany__in=degraycompsec)
if i print it after convert it to list and loop will get this dict :
{'id': 5, 'degraycomany_id': 5, 'nomberhisas': 3, 'date': datetime.date(2020, 9, 25)}
{'id': 8, 'degraycomany_id': 5, 'nomberhisas': 1, 'date': datetime.date(2020, 9, 27)}
{'id': 9, 'degraycomany_id': 5, 'nomberhisas': 1, 'date': datetime.date(2020, 9, 27)}
{'id': 10, 'degraycomany_id': 5, 'nomberhisas': 1, 'date': datetime.date(2020, 9, 27)}
{'id': 13, 'degraycomany_id': 5, 'nomberhisas': 3, 'date': datetime.date(2020, 9, 28)}
{'id': 7, 'degraycomany_id': 6, 'nomberhisas': 2, 'date': datetime.date(2020, 9, 27)}
{'id': 11, 'degraycomany_id': 6, 'nomberhisas': 1, 'date': datetime.date(2020, 9, 27)}
{'id': 6, 'degraycomany_id': 7, 'nomberhisas': 1, 'date': datetime.date(2020, 9, 27)}
{'id': 12, 'degraycomany_id': 7, 'nomberhisas': 1, 'date': datetime.date(2020, 9, 28)}
{'id': 14, 'degraycomany_id': 7, 'nomberhisas': 3, 'date': datetime.date(2020, 9, 28)}
{'id': 15, 'degraycomany_id': 8, 'nomberhisas': 1, 'date': datetime.date(2020, 9, 28)}
so now i want to get sum of every degraycomany_id so i use this queryset:
test1 = MediaSec.objects.filter(date__range=[primary1, primary2], degraycomany__in=degraycompsec).aggregate(Sum("nomberhisas"))
the rusult come :
{'nomberhisas__sum': 18}
i want reslt like this :
{'id': 13, 'degraycomany_id': 5, 'nomberhisas': 9, 'date': datetime.date(2020, 9, 28)}
{'id': 11, 'degraycomany_id': 6, 'nomberhisas': 3, 'date': datetime.date(2020, 9, 27)}
{'id': 14, 'degraycomany_id': 7, 'nomberhisas': 5, 'date': datetime.date(2020, 9, 28)}
{'id': 15, 'degraycomany_id': 8, 'nomberhisas': 1, 'date': datetime.date(2020, 9, 28)}
i want get sum of 'nomberhisas' in every 'degraycomany_id' not the sum of all object
the models :
class MediaSec(models.Model):
degraycomany = models.ForeignKey(DegreyCompany, on_delete=models.CASCADE)
nomberhisas = models.SmallIntegerField()
date = models.DateField()
update_at = models.DateTimeField(auto_now_add=True, blank=1, null=1)
created_at = models.DateTimeField(auto_now_add=True)
class DegreyCompany(models.Model):
withdegrey = models.ForeignKey(Degrey, on_delete=models.CASCADE, null=True, blank=True)
company = models.ForeignKey(Secondary, on_delete=models.CASCADE, null=True, blank=True)
spesial = models.ForeignKey('Spesial', on_delete=models.CASCADE, null=True, blank=True)
name = models.CharField(max_length=200, default=withdegrey.name, null=True, blank=True)
nomberetud = models.PositiveSmallIntegerField(null=True, blank=True)
nomberexist = models.PositiveSmallIntegerField(null=True, blank=True)
femail = models.PositiveSmallIntegerField(null=True, blank=True)
reatrap = models.PositiveSmallIntegerField(null=True, blank=True)
i try this queryset :
test1 = MediaSec.objects.filter(date__range=[primary1, primary2], degraycomany__in=degraycompsec).values('degraycomany_id').annotate(total_price=Sum('nomberhisas'))
the ruslt come :
<QuerySet [{'degraycomany_id': 5, 'total_price': 9}, {'degraycomany_id': 6, 'total_price': 3}, {'degraycomany_id': 7, 'total_price': 5}, {'degraycomany_id': 8, 'total_price': 1}]>
this is near from what i want maybe help