如何在模型中查询 ForeignKey 计算字段的聚合
How do you query a model for an aggregate of a ForeignKey computed field
如何在模型中查询外键 'computed'(自动字段?)字段的聚合
我有两个模型:
class Job(models.Model):
name = models.CharField(…)
class LineItem(models.Model):
job = models.ForeignKey(Job, …)
metric_1 = models.DecimalField(…)
metric_2 = models.DecimalField(…)
metric_3 = models.DecimalField(…)
# Making this a @property, @cached_property, or the like makes no difference
def metric_total(self):
return (self.metric_1 + self.metric_2 * self.metric_3)
在视图中:
class WhosebugView(ListView, …):
model = Job
def get_queryset(self):
return Job.objects
.select_related(…)
.prefetch_related('lineitem_set')
.filter(…).order_by(…)
def get_context_data(self, **kwargs):
context_data = super(WhosebugView, self).get_context_data(**kwargs)
context_data['qs_aggregate'] = self.get_queryset() \
.annotate(
# Do I need to Annotate the model?
).aggregate(
# This works for any of the fields that have a model field type
metric1Total=Sum('lineitem__metric_1'),
metric2Total=Sum('lineitem__metric_2'),
# This will error :
# Unsupported lookup 'metric_total' for AutoField or join on the field not permitted.
# How do I aggregate the computed model field 'metric_total'?
metricTotal=Sum('lineitem__metric_total'),
)
return context_data
当我尝试聚合计算字段时,出现错误:Unsupported lookup 'metric_total' for AutoField or join on the field not permitted.
。如何聚合这些特殊字段?
你也必须计算metric_total
Job.objects.aggregate(
metric1Total=Sum('lineitem__metric_1'),
metric2Total=Sum('lineitem__metric_2'),
<b>metric_total=Sum('lineitem__metric_1') + Sum('lineitem__metric_2')</b>
)
可能的重复:
简而言之,这些 annotate()
和 aggregate()
方法在数据库级别执行,而 metric_total()
“方法” 在数据库级别执行Python级。
如何在模型中查询外键 'computed'(自动字段?)字段的聚合
我有两个模型:
class Job(models.Model):
name = models.CharField(…)
class LineItem(models.Model):
job = models.ForeignKey(Job, …)
metric_1 = models.DecimalField(…)
metric_2 = models.DecimalField(…)
metric_3 = models.DecimalField(…)
# Making this a @property, @cached_property, or the like makes no difference
def metric_total(self):
return (self.metric_1 + self.metric_2 * self.metric_3)
在视图中:
class WhosebugView(ListView, …):
model = Job
def get_queryset(self):
return Job.objects
.select_related(…)
.prefetch_related('lineitem_set')
.filter(…).order_by(…)
def get_context_data(self, **kwargs):
context_data = super(WhosebugView, self).get_context_data(**kwargs)
context_data['qs_aggregate'] = self.get_queryset() \
.annotate(
# Do I need to Annotate the model?
).aggregate(
# This works for any of the fields that have a model field type
metric1Total=Sum('lineitem__metric_1'),
metric2Total=Sum('lineitem__metric_2'),
# This will error :
# Unsupported lookup 'metric_total' for AutoField or join on the field not permitted.
# How do I aggregate the computed model field 'metric_total'?
metricTotal=Sum('lineitem__metric_total'),
)
return context_data
当我尝试聚合计算字段时,出现错误:Unsupported lookup 'metric_total' for AutoField or join on the field not permitted.
。如何聚合这些特殊字段?
你也必须计算metric_total
Job.objects.aggregate(
metric1Total=Sum('lineitem__metric_1'),
metric2Total=Sum('lineitem__metric_2'),
<b>metric_total=Sum('lineitem__metric_1') + Sum('lineitem__metric_2')</b>
)
可能的重复:
简而言之,这些 annotate()
和 aggregate()
方法在数据库级别执行,而 metric_total()
“方法” 在数据库级别执行Python级。