以上是可能的,并且在Django Documentation on Conditional Aggregation中有介绍。
在示例中,他们展示了
from django.db import models
class Client(models.Model):
REGULAR = 'R'
GOLD = 'G'
PLATINUM = 'P'
ACCOUNT_TYPE_CHOICES = (
(REGULAR, 'Regular'),
(GOLD, 'Gold'),
(PLATINUM, 'Platinum'),
)
name = models.CharField(max_length=50)
registered_on = models.DateField()
account_type = models.CharField(
max_length=1,
choices=ACCOUNT_TYPE_CHOICES,
default=REGULAR,
)
然后对其执行聚合:
>>> from django.db.models import IntegerField, Sum
>>> Client.objects.aggregate(
regular=Sum(
Case(
When(account_type=Client.REGULAR, then=1),
output_field=IntegerField()
)
),
gold=Sum(
Case(
When(account_type=Client.GOLD, then=1),
output_field=IntegerField()
)
),
platinum=Sum(
Case(
When(account_type=Client.PLATINUM, then=1),
output_field=IntegerField()
)
)
)
>>> {'regular': 2, 'gold': 1, 'platinum': 3}
所以是的,这是可能的。鉴于您上面的示例,它将大致如下:
from django.db.models import IntegerField, Sum
SomeTable.objects.aggregate(
price_sum=Sum(
Case(
When(other_model__some_field_name='some_value', then='price'),
default=0,
output_field=IntegerField()
)
)
)