Экспорт результатов из django -фильтра - PullRequest
0 голосов
/ 10 марта 2020

Я пытаюсь загрузить результаты из отфильтрованного представления в Django 2.1.

class ListadoEmployeeViewContactoFormularioColombia(FilteredListView):
    filterset_class = ListadoFormulariosFilter
    paginate_by = 10
    model = ContactoInfoColombia
    fields = ['plataformaPrincipal', 'numeroPlataformaPrincipal']

    #context_object_name = 'quiz'
    template_name = 'usuarios/listadoContactoEmployee.html'

Я искал его, но пока не нашел ни одного ответа. Я думал о создании другого класса, в котором я получаю параметры запроса, как показано ниже, но я не думаю, что это хороший способ получить результаты.

class PaymentExportView(FilteredListView):
    filterset_class = ListadoFormulariosFilter
    model = ContactoInfoColombia

    def render_to_response(self, context, **response_kwargs):
        # Could use timezone.now(), but that makes the string much longer
        filename = "{}-export.csv".format(datetime.now().replace(microsecond=0).isoformat())

        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)

        writer = csv.writer(response)
        writer.writerow(['nombre'])
        query = ContactoInfoColombia.objects.filter(numeroPlataformaPrincipal=self.request.GET.get("numeroPlataformaPrincipal"))
        for obj in query:
            writer.writerow([obj.nombre])

        return response
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...