我对"聚合"one_answers"注释"没有经验,更好地与示例交谈我有一个目标:
mediasec = MediaSec.objects.filter(date__range=[primary1, primary2], degraycomany__in=degraycompsec)
如果我在将其转换为列表和循环后打印它,将得到以下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)}
所以现在我想得到每个degraycomany_id的总和,所以我使用这个查询集:
test1 = MediaSec.objects.filter(date__range=[primary1, primary2], degraycomany__in=degraycompsec).aggregate(Sum("nomberhisas"))
结果来了:
{'nomberhisas__sum': 18}
我想要这样的回复:
{'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)}
我想得到每个"degraycomany_id"中"nomberhisas"的总和,而不是所有对象的总和
型号:
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)
我尝试这个查询集:
test1 = MediaSec.objects.filter(date__range=[primary1, primary2], degraycomany__in=degraycompsec).values('degraycomany_id').annotate(total_price=Sum('nomberhisas'))
俄罗斯来了:
<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}]>
这离我想帮助的地方很近