django-tables2 and foreign keys

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.

In your RiskSet definition you have:

But you’re trying to reference a name risks here:

This reference needs to use the related name, riskset. (There might still be other issues here, but this is the first that jumped out at me.)

(Also, I’m assuming that you do have a field named score in the RiskSet model and just didn’t show it here.)

Thanks Ken!

Yes, I was fumbling around throwing everything and trying to see what did/not work, and resulted in that error.

However, even after correcting the accessor, for score, the table still does not display the desired attribute. Similarly, contact_person is correctly accessed and used in the example, but the email address does not appear.

At any rate, I changed up my schema and now can access/display the fields as expected. If any extra table fields have an issue, I’ll jump back to this post.

Thanks so much for your guidance and advice, Ken!