Search code examples
pythondjangodjango-tables2

How to allow sorting by foreign key fields?


Consider the following django-tables2 table:

class ProductMaxIPPortEldCountTable(tables.Table):                             
    vendor = tables.TemplateColumn('{{ record.product.vendor }}')

When I try to sort it by vendor, which is a foreign key in the underlying model, I get "Cannot resolve keyword u'vendor' into field." error. How can I give django-tables2 a hint on how this column is linked to a model field?


Solution

  • Using a Column with an accessor helped:

    class ProductMaxIPPortEldCountTable(tables.Table):                          
        vendor = tables.Column(accessor='product.vendor')