Performance: Response model exclude and include
MEDIUM IMPACT
This affects the size of the JSON response sent to the client, impacting network load and client rendering speed.
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class User(BaseModel): id: int username: str email: str password: str @app.get('/user', response_model=User, response_model_exclude={'password'}) async def get_user(): user = User(id=1, username='alice', email='alice@example.com', password='secret') return user
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class User(BaseModel): id: int username: str email: str password: str @app.get('/user', response_model=User) async def get_user(): user = User(id=1, username='alice', email='alice@example.com', password='secret') return user
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full response with all fields | N/A | N/A | Higher due to larger JSON parsing | [X] Bad |
| Response with exclude/include to limit fields | N/A | N/A | Lower due to smaller JSON parsing | [OK] Good |