Skip to content
Advertisement

I want to change api response json in fastapi | pydantic

I am new in fastapi and please help me!

I got an validation error when I change the default API response comes when I use response_model in fastAPI.

The default API response is simple json like object from response_model.

user.py

JavaScript

schemas.py

JavaScript

Now, when I run this code it gives me errors like below mentioned.

JavaScript

Below is the output i want:-

JavaScript

Thank you so much.

Advertisement

Answer

The object you are returning doesn’t match your response_model. You have two options:

  1. The last line should be return new_user instead of return {"status":True,"data":new_user, "message":"User Created Successfully"}

  2. You response_model should be something like:

    JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement