1

这似乎与这个问题非常相似。单击按 pass_count 排序的链接时,我得到:

无法将关键字 u'pass_count' 解析为字段。选项有:build、build_no

模型.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

表.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"}})

有没有办法解决这个问题?

4

1 回答 1

0

You'll need to convert your data to a dictionary to allow for non-db fields to be sortable.

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)

Note: This will query the database once for each run. A solution to that would require a QuerySet on builds and a loop over all of the builds adding each build's pass_count to a dictionary based on runs.

于 2013-09-04T21:22:59.267 回答