Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ManPython opened this issue Jan 28, 2017 · 4 comments
Open

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

ManPython opened this issue Jan 28, 2017 · 4 comments

Comments

@ManPython
Copy link

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

@ManPython
Copy link
Author

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
Copy link
Owner

mjumbewu commented Feb 11, 2017

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
Copy link
Author

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
Copy link

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

This could go in the readme 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants