django-export-action
django-export-action copied to clipboard
CSV export bytes representation of string
- Django Export Action version:
django-export-action==0.1.1 - Django version:
django==1.11.9 - Python version:
3.6.5 - Operating System: debian stretch (official python docker image)
Description
When I trying to export char field of the model into CSV format I got repr() of the string encoded to bytes instead of the string itself.
b'auth_token__key'
b'5e36616a85ff284433b8e3acb9f919513ac2a90a'
b'ad31300cd28ce281b9ea677c8ffb5e437f697134'
b'7e48a77e8261e115485807f45aab3edd5b47a7e7'
But instead of this, I expect the string value itself.
auth_token__key
5e36616a85ff284433b8e3acb9f919513ac2a90a
ad31300cd28ce281b9ea677c8ffb5e437f697134
7e48a77e8261e115485807f45aab3edd5b47a7e7
What I Did
I trace execution to the export_action.report.list_to_csv_response function. It contains this line
cw.writerow([force_text(s).encode(response.charset) for s in row])
If I remove the encode method call, export works as expected.
def list_to_csv_response(data, title="report", header=None, widths=None):
""" Make 2D list into a csv response for download data.
"""
response = HttpResponse(content_type="text/csv; charset=UTF-8")
cw = csv.writer(response)
for row in chain([header] if header else [], data):
cw.writerow([force_text(s) for s in row])
return response
Maybe this is Python 2 & 3 compatibility issue. I can guess that on Python 2 CSV writer expect str instead of Unicode. And on Python 3 it expects str instead of bytes.
Please, let me know if I can help somehow to resolve it.
Regards, Artem.