django-rest-pandas icon indicating copy to clipboard operation
django-rest-pandas copied to clipboard

how to return a json Response when query_params with 'format=xlsx'

Open wgf4242 opened this issue 5 years ago • 2 comments

If there isn't a 'form' param in request ,I want return an error, but raised an error.

def list(self, request: Request, *args: Any, **kwargs: Any) -> Response:
    if self.request.query_params.get('form') and self.request.query_params.get('format') == 'xlsx':
        return Response({'error' : 'no form'})

error:

Response data is a dict, not a DataFrame!

wgf4242 avatar Jun 23 '19 02:06 wgf4242

Yes, by default the Response object is rendered with the negotiated renderer, regardless of the content. Here are some things you could try:

  1. Throw an exception instead of returning a Response
  2. Explicitly override the "Content-Type" header to be "application/json" (perhaps using Django's HttpResponse)
  3. Generate a small DataFrame with a single row containing the error message (pd.DataFrame([{"error": no form"}])

sheppard avatar Jul 23 '19 01:07 sheppard

image

Hello, could you help me? I am trying to generate an excel and everything is fine but it is not downloaded, it is only generated in the browser or does anyone have any idea why?

image

thmxluis avatar Jul 14 '20 22:07 thmxluis