简体   繁体   English

Django-Tables2-如何为所有列设置默认值?

[英]Django-tables2 - how to set default value for all columns?

Django-tables2 renders double dash in case there is no value in the cell. 如果单元格中没有值,则Django-tables2会呈现双破折号。 We can change it explicitely specifying default attribute for every column. 我们可以更改它,为每列明确指定default属性。

Can I do that for all columns at once? 我可以一次对所有列执行此操作吗?

Of course I tried to override __init__ method but Table.columns can't be modified and Table.base_columns seems to cause the same problem. 当然,我尝试覆盖__init__方法,但是Table.columns无法修改,并且Table.base_columns似乎会引起相同的问题。

def __init__(self,*args,**kwargs):
    for name,col in self.base_columns.items():
        col.default = ''

    super(DopytyTable,self).__init__(*args,**kwargs)

raises 加薪

 col.default = '' AttributeError: can't set attribute 

I haven't figured out how to modify columns inside __init__ method but there is even more simple way to do that. 我还没有弄清楚如何在__init__方法中修改列,但是有更简单的方法可以做到这一点。 Unfortunately it isn't documented but it works correctly for me. 不幸的是,它没有记录在案,但对我来说可以正常使用。

Just add default attribute to class Meta : 只需将default属性添加到class Meta

class Table(Table):
    class Meta:
        default = ''

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

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