使用django table2进行表导出仅生成模型数据,而不会生成访问器列。
下面是我的表和视图配置。任何帮助将不胜感激。
表格
class AgreementPeopleTable(tables.Table):
export_formats = ['csv', 'xls']
#title = tables.Column(linkify=True)
full_name = tables.Column(accessor='employement_episode.contact.full_name', verbose_name='Full Name')
start = tables.Column(accessor='employement_episode.start_date')
end = tables.Column(accessor='employement_episode.end_date')
role = tables.Column(accessor='employement_episode.contact.role_name',verbose_name='Role')
organization = tables.Column(accessor='employement_episode.organization.organization_name',verbose_name='Organization')
title = tables.Column(accessor='agreement.title')
status = tables.Column(accessor='agreement.status')
class Meta:
model = Agreement_People
exclude = ('agreement_people_id', 'employement_episode','agreement')
template_name = 'django_tables2/bootstrap4.html'
attrs = {'class': 'table table-hover'}
查看
class AgreementPeopleListView(ExportMixin, tables.SingleTableView, generic.ListView):
model = Agreement_People
def get_context_data(self, **kwargs):
context = super(AgreementPeopleListView, self).get_context_data(**kwargs)
agreementpeople = Agreement_People.objects.all()
form = AgreementPeopleForm(self.request.GET)
if form.is_valid():
#print("date signed db value", date_signed)
if form.cleaned_data["title"]:
agreementpeople = agreementpeople.filter(agreement__title__icontains=form.cleaned_data["title"])
table = AgreementPeopleTable(agreementpeople.order_by('pk'))
RequestConfig(self.request, paginate={'per_page': 4}).configure(table)
context['table'] = table
context['form'] = AgreementPeopleForm()
return context
Excel输出
Note: Accessor columns are not coming excel output, only columns defined in model are retrieved.
ModelCol1 ModelCol2 ModelCol3
1个XYZ PQR
答案 0 :(得分:0)
我只是added a test进入django-tables2测试套件,以检查访问器列是否正确导出。一切似乎都很好,直到我在关联之后添加了访问器,这些值才没有正确添加到导出中。
所以看来您在django-tables2中发现了一个错误,在this commit中未通过测试。
我将尝试提出修复程序,以供下一次在github的django-tables2问题跟踪器中open an issue进行。如果您可以提供一个 minimum 示例来重现此问题,则确实有帮助,因此请继续删除代码,直到剩下显示该问题所需的最少代码量为止。
更新:好的,我应该立即在测试中看到此错误:django-tables2访问器使用.
作为分隔符,而不是我在上面链接的测试中使用的__
作为分隔符。如果我修改测试,它会像预期的那样工作:updated test