【问题标题】:Django - Override data content of a django-tables2 LinkColumnDjango - 覆盖 django-tables2 LinkColumn 的数据内容
【发布时间】:2013-04-24 17:40:49
【问题描述】:

我使用 django-tables2 LinkColumn 创建一个列,该列调用允许导出表中对象的函数。

forms.py:

class FilesTable(tables.Table):
    id = tables.LinkColumn('downloadFile', args=[A('pk')], verbose_name='Export')

我希望本专栏的内容是下载文件功能的 href :导出为文本,而不是 id。

【问题讨论】:

    标签: python django django-forms django-tables2


    【解决方案1】:

    类似的东西应该可以工作(警告我这里没有 Python,所以它没有经过测试,但你会明白的):

    类 CustomTextLinkColumn(LinkColumn): def __init__(self, viewname, urlconf=None, args=None, kwargs=None,current_app=None,attrs=None,custom_text=None,**额外): super(CustomTextLinkColumn, self).__init__(viewname, urlconf=urlconf, args=args,kwargs=kwargs,current_app=current_app,attrs=attrs,**额外) self.custom_text = custom_text def 渲染(自我,价值,记录,bound_column): return super(CustomTextLinkColumn, self).render(self, self.custom_text 如果 self.custom_text 其他值, 记录,bound_column)

    然后你可以像这样使用它

    id = CustomTextLinkColumn('downloadFile', args=[A('pk')], custom_text='出口',verbose_name='出口',)

    当然,您始终可以使用 TemplateColumn 或向 FilesTable 添加 render_id 方法,但 CustomTextLinkColumn 绝对是最干燥的方法:)

    【讨论】:

      【解决方案2】:

      我无法发表评论,所以我需要添加另一个答案。我将更正“render”调用在参数列表中不应包含“self”。

      class CustomTextLinkColumn(LinkColumn):
        def __init__(self, viewname, urlconf=None, args=None, 
          kwargs=None, current_app=None, attrs=None, custom_text=None, **extra):
          super(CustomTextLinkColumn, self).__init__(viewname, urlconf=urlconf, 
            args=args, kwargs=kwargs, current_app=current_app, attrs=attrs, **extra)
          self.custom_text = custom_text
      
      
        def render(self, value, record, bound_column):
          return super(CustomTextLinkColumn, self).render( 
            self.custom_text if self.custom_text else value, 
            record, bound_column)   
      

      使用,正如 Serafeim 所说:

      id = CustomTextLinkColumn('downloadFile', args=[A('pk')], 
        custom_text='Export', verbose_name='Export', )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多