[英]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
這引起了:
無法設置屬性
它必須能夠以某種方式做到這一點,否則我必須指定所有這些列:
ham = tables.Column(accessor='ham',orderable=False)
你有什么想法?
self.columns
包含BoundColumn
實例。 它們具有一些其他知識(例如,它們在使用它們的表中的屬性名稱),並通過self.column
引用實際定義的Column
實例。 它們還通過一個無setter的屬性公開了該列的orderable
屬性,因此會出現錯誤。 為了動態更改該屬性,必須在基礎列上設置屬性:
self.columns[column].column.orderable = False
# instead of
# self.columns[column].orderable = False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.