django-rest-framework-csv icon indicating copy to clipboard operation
django-rest-framework-csv copied to clipboard

Possible bug: '?format=csv' is appended to URLs in the downloaded CSV

Open dfarre opened this issue 8 years ago • 3 comments

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.

dfarre avatar Oct 07 '16 15:10 dfarre

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)

mjumbewu avatar Nov 05 '16 11:11 mjumbewu

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 avatar Nov 07 '16 11:11 dfarre

@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 the get_url method to exclude the format parameter.
  • If you're using a subclass of HyperlinkedRelatedSerializer, then override the serializer_related_field attribute, and set it to the HyperlinkedRelatedField 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.

mjumbewu avatar Jan 11 '17 21:01 mjumbewu