django-rest-framework-csv
django-rest-framework-csv copied to clipboard
Possible bug: '?format=csv' is appended to URLs in the downloaded CSV
We are with the last version using the PaginatedCSVRenderer
, specified in the settings. CSVs are not correctly downloaded if we either put 'format=csv' in the query string or use the drop-down menu for choosing the format in the browsable API; then '?format=csv' is appended to the URLs in the downloaded CSVs - using hyperlinked serializers. URLs are correct if we specify 'text/csv' in the accept header and not in the query string.
If I'm understanding correctly, you're seeing the filenames contain "?format=csv" when it's supplied in the URL. There's currently no way within the renderer to specify the name of the generated file. That might be something that you'd want to handle within the view by adding a Content-disposition
header to the response (e.g. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Examples)
Thanks for your reply. It is not the file name but the values within the file that are URL's (hyperlinks in the browsable API); e.g., the value of "user" could be "http://api.mysite/users/22/", and wrongly appear as "http://api.mysite/users/22/?format=csv".
@dfarre This is actually because of the way that DRF constructs links to relations. I could think of a few things you could do:
- Create a subclass of
HyperlinkedRelatedField
and override theget_url
method to exclude the format parameter. - If you're using a subclass of
HyperlinkedRelatedSerializer
, then override theserializer_related_field
attribute, and set it to theHyperlinkedRelatedField
subcless you created above.
- OR -
- Set CSV as the default format so that you don't have to include the format parameter
- OR -
- Request the resource with an HTTP
Accept
header set to"text/csv"
so that you can omit the format parameter.