Search code examples
djangodjango-tables2

django-tables2 - accessing values of other columns in a table class


Let's assume I have the following table class:

class TestTable(tables.Table):
    id = tables.Column()
    description = tables.Column()

    def render_description(self, value):
        return mark_safe('''<a href=%s>%s</a>''' % (???, value))

Is it possible to access the value of the column "id" in the render method, so that I can build up a link which leads to the id but shows the text which depends on the 'description'-field?

Thanks in advance!


Solution

  • From a quick glance at the docs for render_FOO it looks like you can just do:

    class TestTable(tables.Table):
        id = tables.Column()
        description = tables.Column()
    
        def render_description(self, value, record):
            return mark_safe('''<a href=%s>%s</a>''' % (record.id, value)
    

    Not sure of the exact shape of a row record, so it might be record['id'], the link to the docs should help with exploration...