覆盖相关字段的“render_FOO”
Override `render_FOO` for a related field
我有一个 table 这样的:
import django_tables2 as tables
from .models import MyModel
class MyTable(tables.Table):
class Meta:
model = MyModel
fields = ['myfield', 'relatedtable.otherfield']
因为我不能有 render_relatedtable.otherfield
并且 render_relatedtable__otherfield
不起作用,我怎样才能为 relatedtable.otherfield
覆盖 render_<column_name>
或 value_<colum_name>
?有可能吗?
我尝试了以下方法,但 none 有效:
覆盖 __init__()
中的属性
class MyTable(tables.Table):
class Meta:
model = MyModel
fields = ['myfield', 'relatedtable.otherfield']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.columns['relatedtable.otherfield'].render = myrenderfnc
# and/or
self.columns['relatedtable.otherfield'].column.render = myrenderfnc
自定义列
class MyColumn(tables.Column):
def render(self, record):
return getattr(record, 'relatedtable.otherfield')
class MyTable(tables.Table):
class Meta:
model = MyModel
fields = ['myfield']
otherfield = MyColumn()
'Renaming'列
class MyTable(tables.Table):
class Meta:
model = MyModel
fields = ['myfield']
def __init__(self, *args, **kwargs):
exclude = ['relatedtable.otherfield']
extra_columns = [('otherfield', self.base_columns['relatedtable.otherfield')]
super().__init__(*args, exclude=exclude, extra_columns=extra_columns, **kwargs)
self.columns['relatedtable.otherfield'].render = myrenderfnc
# and/or
self.columns['relatedtable.otherfield'].column.render = myrenderfnc
一种方法是使用访问器显式定义列,然后在 render_FOO
方法名称中使用 列名称,如下所示:
class MyTable(tables.Table):
otherfield = MyColumn(accessor='relatedtable.otherfield')
class Meta:
model = MyModel
fields = ['myfield']
def render_otherfield(self, record, value):
return value
您的 'custom column' 示例也应该有效。
我有一个 table 这样的:
import django_tables2 as tables
from .models import MyModel
class MyTable(tables.Table):
class Meta:
model = MyModel
fields = ['myfield', 'relatedtable.otherfield']
因为我不能有 render_relatedtable.otherfield
并且 render_relatedtable__otherfield
不起作用,我怎样才能为 relatedtable.otherfield
覆盖 render_<column_name>
或 value_<colum_name>
?有可能吗?
我尝试了以下方法,但 none 有效:
覆盖 __init__()
中的属性
class MyTable(tables.Table):
class Meta:
model = MyModel
fields = ['myfield', 'relatedtable.otherfield']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.columns['relatedtable.otherfield'].render = myrenderfnc
# and/or
self.columns['relatedtable.otherfield'].column.render = myrenderfnc
自定义列
class MyColumn(tables.Column):
def render(self, record):
return getattr(record, 'relatedtable.otherfield')
class MyTable(tables.Table):
class Meta:
model = MyModel
fields = ['myfield']
otherfield = MyColumn()
'Renaming'列
class MyTable(tables.Table):
class Meta:
model = MyModel
fields = ['myfield']
def __init__(self, *args, **kwargs):
exclude = ['relatedtable.otherfield']
extra_columns = [('otherfield', self.base_columns['relatedtable.otherfield')]
super().__init__(*args, exclude=exclude, extra_columns=extra_columns, **kwargs)
self.columns['relatedtable.otherfield'].render = myrenderfnc
# and/or
self.columns['relatedtable.otherfield'].column.render = myrenderfnc
一种方法是使用访问器显式定义列,然后在 render_FOO
方法名称中使用 列名称,如下所示:
class MyTable(tables.Table):
otherfield = MyColumn(accessor='relatedtable.otherfield')
class Meta:
model = MyModel
fields = ['myfield']
def render_otherfield(self, record, value):
return value
您的 'custom column' 示例也应该有效。