MichaelPlante
MichaelPlante

Reputation: 462

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?

Upvotes: 0

Views: 1773

Answers (1)

Jieter
Jieter

Reputation: 4229

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...

Upvotes: 3

Related Questions