I'm using djangotables2 for showing my tables in django. I dont wanna show a model itself but something more complicated: a query object, with annotates and counts and more aggregate functions. However when the table is rendered, it shows just the fields of the model and not the fields of my query object. Well, also my model has new attributes so it is not a basic model. Don't know how to fix that.
// view //
serial_number_list = TestSerialNumber.objects.filter(test_pool=test_pool).annotate(test_runs=Count('testrun')).order_by(o)
table = TestSerialNumberTable(serial_number_list)
RequestConfig(request).configure(table)
// models.py //
class TestSerialNumber(models.Model):
serial_number = models.ForeignKey("core.SerialNumber", on_delete=models.PROTECT)
test_pool = models.ForeignKey("TestPool", blank=True, null=True)
def __unicode__(self):
return self.serial_number.serial_number
def status(self):
try:
test_result = self.testresult_set.latest('report')
except TestResult.DoesNotExist:
return 'unknown'
else:
return test_result.test_status.name
class Meta:
db_table = 'ats2_serialnumber'
// tables.py //
import django_tables2 as tables
from ats2.models import TestSerialNumber
class TestSerialNumberTable(tables.Table):
class Meta(object):
model = TestSerialNumber
Thanks in advance!!!
According to this, you need to add those fields (which aren't in models but added in queryset) in table class:
import django_tables2 as tables
from ats2.models import TestSerialNumber
class TestSerialNumberTable(tables.Table):
test_runs= tables.Column()
class Meta(object):
model = TestSerialNumber