简体   繁体   English

Django-tables2:更改__init __()中多个列的属性

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

I can't figure out how to change attribute for multiple columns in one for loop. 我不知道如何在for循环中更改多列的属性。

I want to set orderable=False to multiple columns. 我想将orderable=False设置为多列。 The only way which works is to explicitely define all these columns so I can add orderable=False to constructor. 唯一有效的方法是显式定义所有这些列,以便可以向构造函数添加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

This raises: 这引起了:

can't set attribute 无法设置属性

It has to be able to do it somehow, otherwise I would have to specify all of those columns: 它必须能够以某种方式做到这一点,否则我必须指定所有这些列:

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

Do you have any ideas? 你有什么想法?

self.columns contains instances of BoundColumn . self.columns包含BoundColumn实例。 These have some additional knowledge (eg their own attribute name within the table they are used in) and refer to the actual defined Column instance via self.column . 它们具有一些其他知识(例如,它们在使用它们的表中的属性名称),并通过self.column引用实际定义的Column实例。 They also expose that column's orderable attribute via a setter-less property, hence the error. 它们还通过一个无setter的属性公开了该列的orderable属性,因此会出现错误。 In order to dynamically change that property, you have to set the attribute on the underlying column: 为了动态更改该属性,必须在基础列上设置属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM