Экспорт таблиц с использованием таблиц django2, производящих только данные модели, но не столбцы доступа.
Ниже приведены мои таблицы и конфигурации представлений.Любая помощь будет принята с благодарностью.
Таблица
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