I'm using the django_tables2 to show a list of "repair details". So far so good. However I want to add a new field (it's not in the model): a list of service reports. The relation is 1 to many. and show the serial number which is in another model. It's quite difficult since I add the field but the render_NEW_FIELD doesn't work and i couldn't reach the list of service reports since in the table model I have just access to the record and not to the repair_detail model. I'm using django_tables 0.15
repair/models.py
class RepairDetail(models.Model):
repair = models.ForeignKey('Repair')
repair_detail_number = models.IntegerField()
article_code = models.CharField(max_length=30)
registered_date = models.DateField(null=True)
registered_uid = models.IntegerField(null=True)
registered_initials = models.CharField(max_length=2, null=True)
customer_reference = models.CharField(max_length=20, null=True)
db2_recnum = models.IntegerField()
def __unicode__(self):
return str(self.repair_detail_number)
class ServiceReport(models.Model):
repair_detail = models.ForeignKey('RepairDetail')
status = models.ForeignKey('ServiceReportStatus', on_delete=models.PROTECT)
serial_number = models.ForeignKey('core.SerialNumber')
customer = models.ForeignKey('core.Company')
contact = models.ForeignKey('core.Contact', null=True, on_delete=models.SET_NULL)
project = models.ForeignKey('core.Project')
project_code = models.CharField(max_length=4)
identifier = models.CharField(max_length=1)
repair_date = models.DateField(null=True)
repair_uid = models.IntegerField(null=True)
repair_initials = models.CharField(max_length=2, null=True)
booking_date = models.DateField(null=True)
core/models.py
class SerialNumber(models.Model):
product = models.ForeignKey("Product")
panel = models.ForeignKey("Panel", null=True, blank=True, default = None)
serial_number = models.CharField(max_length=30, default='')
manifest = models.TextField(null=True)
def __unicode__(self):
return self.serial_number
repair/tables.py
class RepairDetailTable(tables.Table):
#serials = tables.Column(accessor='servicereport.serialnumber.serialnumber')
serial = tables.Column()
def __init__(self, *args, **kwargs):
super(RepairDetailTable, self).__init__(*args, **kwargs)
class Meta(object):
model = RepairDetail
fields = ('id', 'repair_detail_number', 'article_code', 'registered_date', 'registered_initials', 'customer_reference', 'serials')
attrs = {'class': 'table table-striped table-bordered protonic'}
empty_text = "No records found."
If you want (1:N instead of N:1) to concatenate all related models in the table cell try to do this in your model layer by setting property, something like this:
class RepairDetail(models.Model):
# ...
@property
def service_reports_list(self):
return self.servicereport_set.all()
Define your own column (inherit tables.Column
) and override render
method
Add it to your table class:
class RepairDetailTable(tables.Table):
# ...
service_reports_list = YourOwnColumn(some_kwargs)
# ...