StreamingHttpResponse

class ExportTrsanctionView(LoginRequiredMixin, View):
    def get(self, request, *args, **kwargs):
        f = TransactionFilter(request.session.get('export_journal'))
        queryset = f.qs.prefetch_related(
            'journal_set' , 'journal_set__account'
        ).filter(journal__account__owner=request.user).distinct()


        pseudo_buffer = Echo()
        writer = csv.writer(pseudo_buffer)
        writer.writerow(['Date', 'Account', 'balance', 'transaction type' , 'comment'])

        response = StreamingHttpResponse(
            streaming_content=(writer.writerow(
                    [transaction.date , journal.account , journal.balance , journal.transaction_type , transaction.comment ]
                )  for transaction in queryset for journal in transaction.journal_set.all() 
            ),
        content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="journal{}.csv"'.format(timezone.now().isoformat())
        return response

when i try to download data the header not included and i don’t know why
the header should contain this line

        writer.writerow(['Date', 'Account', 'balance', 'transaction type' , 'comment'])

I don’t know what this Echo class is you’re calling pseudo_buffer, or what its behavior is, but you are writing to it outside the context of your StreamingHttpResponse object. So unless it’s a real buffer and not truly a pseudo buffer, you’re not writing the data out to the stream.

1 Like