Django annotate and values(): extra field in 'group by' causes unexpected results
问题 I must be missing something obvious, as the behavior is not as expected for this simple requirement. Here is my model class: class Encounter(models.Model): activity_type = models.CharField(max_length=2, choices=(('ip','ip'), ('op','op'), ('ae', 'ae'))) cost = models.DecimalField(max_digits=8, decimal_places=2) I want to find the total cost for each activity type. My query is: >>> Encounter.objects.values('activity_type').annotate(Sum('cost')) Which yields: >>> [{'cost__sum': Decimal("140.00")