Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

How to unify the response format in FastAPI while preserving Pydantic data models?

$
0
0

In FastAPI, I am using SQLAlchemy and Pydantic to return data.

@router.get("/1", response_model=User)def read_user(db: Session = Depends(get_db)):db_user = user_module.get_user(db, user_id="1")if db_user is None:raise HTTPException(status_code=404, detail="User not found")return db_user

This approach helps me standardize the returned model, but I want to unify the response format for all APIs to {"code": 0, "msg": "success", "data": {...}}, so that the User model from the original return model is placed within the "data" field, making it easier for frontend management.

I attempted to use FastAPI middleware for implementation, but it doesn't recognize the User return model in Swagger and other documentation. If I redefine a generic Pydantic return model with nested models, I cannot manipulate the SQLAlchemy returned data model into the desired User model.

Is there any way to solve my requirement or are there any better solutions?

To unify the response format in FastAPI to {"code": 0, "msg": "success", "data": {...}}, while preserving Pydantic data models and ensuring proper recognition in Swagger and other documentation.


Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>