pydantic-core
pydantic-core copied to clipboard
validate_python / model_validate: exclude
Lazy loaded orm fields are loaded inadvertently by model_validate (which calls validate_python). I'd like to be able to exclude fields from model_validate.
Previously discussed here https://github.com/pydantic/pydantic/discussions/6861.
With the release of pydantic v2, is it possible to load a model and exclude certain fields when loading using the new
model_validatemethod? I was hoping the newcontextparameter was able to achieve this, but it looks this does not quite do what I was thinking.
(cross post of https://github.com/pydantic/pydantic/issues/8192)
fastapi's response_model_exclude requires this too.
You can also use the path operation decorator parameters
response_model_includeandresponse_model_exclude.They take a
setofstrwith the name of the attributes to include (omitting the rest) or to exclude (including the rest).This can be used as a quick shortcut if you have only one Pydantic model and want to remove some data from the output.
It's used a fair bit too: https://github.com/search?q=language%3Apython+%2Fresponse_model_exclude%3D%5C%7B%2F&type=code
Going to close this as a dupe - I think the pydantic version of this will get more eyes / traction, and potentially more support from the community.