mjumbewu / django-rest-framework-csv

CSV Tools for Django REST Framework
BSD 2-Clause "Simplified" License
364 stars 89 forks source link

Change comma separator from ',' to ';' #60

Open ManPython opened 7 years ago

ManPython commented 7 years ago

How we can easy change comma separator from ',' to ';' in .csv file?

ManPython commented 7 years ago

In parsers.py we have line 39 delimiter = parser_context.get('delimiter', ',') changed to delimiter = parser_context.get('delimiter', ';') but no reaction

Soo..I sugesting maybe to made 2 options

     'DEFAULT_PARSER_CLASSES': (
         'rest_framework.parsers.JSONParser',
         'rest_framework_csv.parsers.CSVParser',
         'rest_framework_csv.parsers.CSVParser2',
     )

rest_framework_csv.parsers.CSVParser with ('delimiter', ',') rest_framework_csv.parsers.CSVParser2 with ('delimiter', ';')

mjumbewu commented 7 years ago

You're looking to change the CSV output to have ; as the delimeter, yes? That would be controlled in the renderer rather than the parser.

To create a renderer that uses semicolons, you could do something like:

class MyCustomRenderer (CSVRenderer):
    writer_opts = dict(
        delimiter=';',
    )

Then you could use that renderer on a per-view basis, or set it in the DEFAULT_RENDERER_CLASSES setting.

ManPython commented 7 years ago

Yes, thank you for correct way. For future, on Python27

class CSVRenderer2 (CSVRenderer): writer_opts = dict( delimiter= str(';'), ) coz

Exception Value: "delimiter" must be string, not unicode

By my opinion this should be done as: response = Response(data, status=None, template_name=None, headers=None, content_type='text/csv', content_disposition = 'attachment', filename ='my_filename', delimeter =';') in case that format = csv format = self.request.query_params.get('format', None) if format == 'csv': by default if REST_FRAMEWORK = { 'DEFAULT_RENDERER_CLASSES': ( 'rest_framework.renderers.BrowsableAPIRenderer', 'rest_framework_csv.renderers.CSVRenderer',

Crocmagnon commented 5 years ago
class MyCustomRenderer (CSVRenderer):
    writer_opts = dict(
        delimiter=';',
    )

This could go in the readme 🙂