Skip to main content
 首页 » 编程设计

python之如何允许按外键字段排序

2024年10月25日17kerrycode

考虑以下 django-tables2 表:

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

当我尝试按 vendor(底层模型中的外键)对其进行排序时,我得到“无法将关键字 u'vendor' 解析为字段”。错误。我如何向 django-tables2 提示此列如何链接到模型字段?

请您参考如下方法:

将 Column 与 accessor 一起使用有助于:

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