这似乎与this question非常相似。单击链接以按pass_count排序时,我得到:
无法将关键字u'pass_count'解析为字段。选择是:build,build_no
models.py:
class Run(DashboardBaseModel):
class Meta:
db_table = 'runs'
build_no = models.CharField(max_length=200, db_index = True)
release = models.CharField(max_length=200, db_index = True)
extra_fields = [
'pass_count',
]
@property
def pass_count(self):
passes = 0
for build in self.build_set.all():
passes += build.pass_count
return passes
tables.py:
class CombineTable(tables.Table):
build_no = tables.LinkColumn('run', args=[A('release'), A('id')], verbose_name="Build")
pass_count = tables.Column(verbose_name="Passed", attrs={"td": {"class": "num"}, "th": {"class": "num"}})
有办法解决这个问题吗?
答案 0 :(得分:0)
您需要将数据转换为字典,以允许非db字段进行排序。
runs = Run.objects.all()
table_data = []
for run in runs:
passes = 0
for build in run.build_set.all():
passes += build.pass_count
table_data.append(dict(
build_no=run.build_no,
release=run.release,
pass_Count=passes,
))
table = CombineTable(table_data)
注意:这将为每次运行查询数据库一次。解决方案需要在构建上使用QuerySet,并在所有构建上循环,根据运行将每个构建的pass_count添加到字典中。