django-tables2:更改外键列的默认列标题
django-tables2: Changing default column title of a foreign key column
如何在 django-tables2 中更改外来值的列标题?
型号
class Employer(models.Model):
name = models.CharField(verbose_name='Name', max_length=16)
class Employee(models.Model):
name = models.CharField(verbose_name='Name', max_length=16)
employer = models.ForeignKey(Employer, verbose_name='Employer')
Table
class EmployeeTable(tables.Table)
class Meta:
model = EmployeeTable
fields = ('name', 'employer.name')
输出
Name | Name
Bill | Steve
我想覆盖默认列 headers 以提供更有意义的信息。
首选输出
Employee name | Employer name
Bill | Steve
Table(不工作)
class EmployeeTable(tables.Table)
name = tables.Column(verbose_name='Employee name') # <-- Works!
employer.name = tables.Column(verbose_name='Employer name') # <-- Doesn't work!
class Meta:
model = EmployeeTable
fields = ('name', 'employer.name')
我找到了解决办法。你必须使用 Accessor
.
from django_tables2.utils import Accessor
class EmployeeTable(tables.Table)
name = tables.Column(verbose_name='Employee name')
employer_name = tables.Column(verbose_name='Employer name', orderable=False, accessor=Accessor('employer.name'))
class Meta:
model = EmployeeTable
fields = ('name', 'employer_name')
如何在 django-tables2 中更改外来值的列标题?
型号
class Employer(models.Model):
name = models.CharField(verbose_name='Name', max_length=16)
class Employee(models.Model):
name = models.CharField(verbose_name='Name', max_length=16)
employer = models.ForeignKey(Employer, verbose_name='Employer')
Table
class EmployeeTable(tables.Table)
class Meta:
model = EmployeeTable
fields = ('name', 'employer.name')
输出
Name | Name
Bill | Steve
我想覆盖默认列 headers 以提供更有意义的信息。
首选输出
Employee name | Employer name
Bill | Steve
Table(不工作)
class EmployeeTable(tables.Table)
name = tables.Column(verbose_name='Employee name') # <-- Works!
employer.name = tables.Column(verbose_name='Employer name') # <-- Doesn't work!
class Meta:
model = EmployeeTable
fields = ('name', 'employer.name')
我找到了解决办法。你必须使用 Accessor
.
from django_tables2.utils import Accessor
class EmployeeTable(tables.Table)
name = tables.Column(verbose_name='Employee name')
employer_name = tables.Column(verbose_name='Employer name', orderable=False, accessor=Accessor('employer.name'))
class Meta:
model = EmployeeTable
fields = ('name', 'employer_name')