Reputation: 565
I am using FastApi and My responseModel is being ignored. I am attempting to NOT return the password field in the response. Why does FastApi ignore my responsemodel definition?
Here is my api post method:
@app.post("/users")
def create_users(user: schemas.UserCreate, db: Session = Depends(get_db), response_model=schemas.UserOut):
new_user = models.User(**user.dict())
print(new_user)
db.add(new_user)
db.commit()
db.refresh(new_user)
return new_user
here is the postman return I should not see the password field:
{
"password": "sdmfsladfj",
"email": "[email protected]",
"created_at": "2022-02-05T00:17:11.010020-06:00",
"id": 10
}
Here is my responseModel definition in my schemas.py file:
class UserOut(BaseModel):
id: int
email: EmailStr
created_at: datetime
class Config:
orm_mode = True
anyio==3.5.0
asgiref==3.5.0
click==8.0.3
dnspython==2.2.0
email-validator==1.1.3
emailvalidator==0.3
fastapi==0.73.0
greenlet==1.1.2
h11==0.13.0
idna==3.3
psycopg2-binary==2.9.3
pydantic==1.9.0
sniffio==1.2.0
SQLAlchemy==1.4.31
sqlalchemy2-stubs==0.0.2a19
sqlmodel==0.0.6
starlette==0.17.1
typing_extensions==4.0.1
utcnow==0.3.0
uvicorn==0.17.1
Upvotes: 0
Views: 254
Reputation: 52902
response_model
is an argument to the view decorator (since it's metadata about the view itself), not to the view function (which takes arguments that are necessary for how to process the view):
@app.post("/users", response_model=schemas.UserOut):
async def ...
Upvotes: 1