Using Django 4.2.11, django-tables2:
For some reason, my table is not rendering the values held in related tables via foreignkeys.
models.py
class Supplier(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False)
name = models.CharField(max_length=128,
blank=False,
unique=True)
class ContactPerson(models.Model):
description = "A contact person for a supplier."
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False
)
supplier = models.ForeignKey(Supplier, on_delete=CASCADE, related_name="contact_person")
name = models.CharField(max_length=64, blank=False)
email = models.EmailField(blank=False)
phone = PhoneNumberField()
website = models.URLField(null=True, blank=True)
class RiskSet(models.Model):
name = "RiskSet and thresholds for a supplier."
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False
)
supplier = models.ForeignKey(
Supplier,
on_delete=CASCADE,
related_name="riskset"
)
#tables.py
class SupplierTable(tables.Table):
name = tables.Column(accessor='name', orderable=True)
#country = tables.Column(accessor='address__country', verbose_name='Country', orderable=True)
score = tables.Column(accessor='risks__score', verbose_name='Risk Score', orderable=True)
email = tables.Column(accessor='contact_person.email', verbose_name='Contact Email', orderable=True)
class Meta:
model = Supplier
template_name = 'django_tables2/bootstrap.html'
fields = ('name', 'score', 'email',)
def render_name(self, value, record):
detail_url = reverse('supplier_details', kwargs={'pk': record.pk})
return format_html('<a href="{}">{}</a>', detail_url, value)
I’ve confirmed that there is data in the db, and that there are entities in the RiskSet and ContactPerson tables linked to the Supplier. The screenshot below confirms that by showing (a) the table in question as it renders in the browser (b) the id of the Supplier object, and (c) the id of the RiskSet object with the linked Supplier as the RIskSet’s ‘supplier’ field.
I’ve used the guidance in the django-tables2 documentation regarding accessor (hence the double-underscores), and I’ve tried dots instead.
Any advice? I’m at a total loss here.