pydantic-core icon indicating copy to clipboard operation
pydantic-core copied to clipboard

validate_python / model_validate: exclude

Open mustafa0x opened this issue 1 year ago • 1 comments

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_validate method? I was hoping the new context parameter 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)

mustafa0x avatar Nov 27 '23 19:11 mustafa0x

fastapi's response_model_exclude requires this too.

You can also use the path operation decorator parameters response_model_include and response_model_exclude.

They take a set of str with 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

mustafa0x avatar Nov 28 '23 04:11 mustafa0x

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.

sydney-runkle avatar Aug 16 '24 17:08 sydney-runkle