Django-tables2:更改 __init__() 中多列的属性

Django-tables2: Change attribute for multiple columns in __init__()

我不知道如何在一个 for 循环中更改多列的属性。

我想将 orderable=False 设置为多列。唯一可行的方法是明确定义所有这些列,以便我可以将 orderable=False 添加到构造函数。

class PizzaTable(tables.Table):

    class Meta:
        template_name = 'django_tables2/bootstrap-responsive.html'
        model = Pizza
        fields = ['created', 'ham', 'olives', 'corn', 'price',]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        unorderable_columns = ['ham', 'olives', 'corn',]
        for column in unorderable_columns:
            self.columns[column].orderable = False

这引发了:

can't set attribute

它必须能够以某种方式完成,否则我将不得不指定所有这些列:

ham = tables.Column(accessor='ham',orderable=False)

你有什么想法吗?

self.columns 包含 BoundColumn 的实例。它们有一些额外的知识(例如它们在 table 中使用的它们自己的属性名称)并通过 self.column 引用实际定义的 Column 实例。他们还通过 setter-less 属性 公开该列的 orderable 属性,因此出现错误。为了动态更改 属性,您必须在基础列上设置属性:

 self.columns[column].column.orderable = False
 # instead of 
 # self.columns[column].orderable = False