Search code examples
pythondjangodjango-tables2

Populate django table column across multiple levels of foreign keys


I need to populate a column of my table with an property from a model separated from the table model by multiple levels of foreign keys. When I attempt to access the property, I just get a dash for every row instead of the value of the property.

From tables.py:

class AssignmentsTable(tables.Table):
    ...
    full_name = tables.Column(accessor='assignment.participant.full_name', verbose_name='First & Last Name (Participant)')
    ...

    class Meta:
        model = AssignmentDetail
        sequence = (..., "full_name", ...)

From models.py:

class AssignmentDetail(models.Model):
    assignment = models.ForeignKey(Assignment)
    ...

class Assignment(models.Model):
    ...
    participant = models.ForeignKey(Account, blank=True, null=True)

class Account(models.Model):
    first_name = models.CharField(max_length=50, blank=True, null=True)
    last_name = models.CharField(max_length=50, blank=True, null=True)
    ...
    @property
    def full_name(self):
        return '%s %s' % (self.first_name, self.last_name)

Is this something that can be done with django tables, and if so, how?


Solution

  • This should work like you showed.

    I just checked in the django-tables2 example: table, models.

    Screenshot of column spanning multiple relations

    I am not sure what prevents your example from functioning properly though...