Question

This seems to be very similar to this question. When clicking the link to sort by pass_count I get:

Cannot resolve keyword u'pass_count' into field. Choices are: 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"}})

Is there a way to work around this?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top