Django - 覆盖django-tables2 LinkColumn的数据内容 [英] Django - Overide data content of a django-tables2 LinkColumn

查看:200
本文介绍了Django - 覆盖django-tables2 LinkColumn的数据内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use django-tables2 LinkColumn to create a column that call a function that allow the export of the object in the table.

forms.py:

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

我想这个列的内容是href到downloadFile函数:导出为文本,而不是id。

I would like the content of this column to be the href to downloadFile function with: Export as the text, not the id.

推荐答案

应该是工作(警告我没有Python在这里没有测试,但你会得到这个想法):

Something like that should be working (warning I don't have Python here so it's not tested but you'll get the idea):


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, 
      self.custom_text if self.custom_text else value, 
      record, bound_column)    

然后你可以使用它像


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

当然可以随时使用TemplateColumn或添加一个render_id方法到您的FilesTable,但绝对的CustomTextLinkColumn是最DRY方法:)

Of course you could always use a TemplateColumn or add a render_id method to your FilesTable but definitely the CustomTextLinkColumn is the most DRY method :)

这篇关于Django - 覆盖django-tables2 LinkColumn的数据内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆