Search code examples
djangodjango-tables2

how use super in django tables2 render_*


i create new Column and add customize render as below

class PriceColumn(django_tables2.Column):
    def render(self, value):
        if isinstance(value, int) or isinstance(value, float):
            self.attrs['td']['title'] = f'{round(value, 2):,}'
            return number_convertor_to_milion(value)
        return '---

then i used it for field

weekly_returns = PriceColumn(verbose_name=_('Weekly Returns'))

def render_weekly_returns(self, value,**kwargs):
    final_result = value*100
    // i want to call super().render() like below
    return super().render(final_result,**kwargs)

i want to call super as in code writed but gives error

AttributeError: 'super' object has no attribute 'render'

how can do this?


Solution

  • In your case super() is referring to the class it's in, which is the MyTable(tables.Table) class, not the intended PriceColumn(Column) class.

    You can fix one of 2 ways, call to the Class method directly;

    def render_weekly_returns(self, value,**kwargs):
        final_result = value*100
        return PriceColumn.render(final_result,**kwargs)
    

    or I would probably recommend just adding the return method instructions into your render_weekly_returns() method as it's going to be easier to read in the future.

    def render_weekly_returns(self, value,**kwargs):
        final_result = value*100
        if isinstance(final_result, int) or isinstance(final_result, float):
            self.attrs['td']['title'] = f'{round(final_result, 2):,}'
            return number_convertor_to_milion(final_result)
        return '---'