【发布时间】:2015-10-23 21:06:55
【问题描述】:
我的模型如下所示:
class MyStat(Model):
metric = ForeignKey(MetricType)
source = ForeingKey(SourceType)
total_units = IntegerField()
metric_units = IntegerField()
objects = MyQuerySet().as_manager()
查询在其一段代码中执行以下操作
rollup = self.order_by().values(source.name)\.
annotate(mu=Sum('metric_units'), tu=Sum('total_units'))
这很好用。但是,我想根据所使用的 metric 类型修改其中的一些注释。不知何故,创建一个注释略有不同的数据库端条件(下面的伪代码):
if metric is some_metric:
.annotate(mu=.8*Sum('metric_units'))
else:
.annotate(mu=Sum('metric_units'))
显然上述语法无效,但我想知道是否有办法设置我的查询集以在数据库端执行类似的操作
【问题讨论】:
标签: python sql django django-models