django-rest-pandas
django-rest-pandas copied to clipboard
how to return a json Response when query_params with 'format=xlsx'
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!
Yes, by default the Response
object is rendered with the negotiated renderer, regardless of the content. Here are some things you could try:
- Throw an exception instead of returning a
Response
- Explicitly override the "Content-Type" header to be "application/json" (perhaps using Django's
HttpResponse
) - Generate a small DataFrame with a single row containing the error message (
pd.DataFrame([{"error": no form"}]
)
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?