Comments (8)
We can leave this open - it's a pretty heavily requested feature, so I think it'll be great to leave this open as a feature request. I'll work on an API design. I think this can fit into the scope of v2.9!
from pydantic.
It should be a new model that doesn't include those fields at all.
I have tried something like:
exclude = [...] ## An array with the names of those fields to exclude
for field_name in list(new_model.model_fields.keys()):
if field_name in exclude:
new_model.model_fields.get(field_name).exclude = True
and even:
exclude = [...] ## An array with the names of those fields to exclude
for field_name in list(new_model.model_fields.keys()):
if field_name in exclude:
new_model.model_fields.pop(field_name)
but when using the newly-created model, the undesired fields are still there, and even populated.
from pydantic.
Yep, gotcha. This reminds me of #6699
from pydantic.
Thank you for your answer, @sydney-runkle.
After some back-and-forth attempts, marking the fields as exclude = True
makes the new model have them also excluded.
for field_name in list(base_model.model_fields.keys()):
if field_name in exclude:
base_model.model_fields.get(field_name).exclude = True
new_model = create_model('MyNewModel', __base__=base_model)
However, I'm really surprised Pydantic doesn't provide a method to exclude fields in an inherited model out of the box, instead of doing all of this handcrafting work.
from pydantic.
Thanks a lot, @sydney-runkle!
from pydantic.
Sorry for the mention, @sydney-runkle.
Do you know if there's a way of doing this? It's a little important for us.
from pydantic.
Do you want to exclude said fields just for serialization, or during validation as well?
from pydantic.
We can mark the above answer as the solution, at least for my specific problem.
from pydantic.
Related Issues (20)
- TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard' HOT 4
- CPython 3.13 warns on FutureRef._evaluate/typing._eval_type not passing `type_params` HOT 5
- Constant value in a Pydantic V2 field does not allow an object value HOT 1
- `typing.Unpack` support: Unpack typed dict for keyword argument typing HOT 4
- unlucky md5 checksum value is parsed to inf HOT 5
- Python 3.12 support in 1.10.x? HOT 14
- pydantic.v1.error_wrappers isnβt present in Pydantic 1.10.15 HOT 2
- Request: built-in `TypeAlias` for a Pydantic serializable object HOT 3
- Pydantic architecture docs HOT 1
- Support for adding custom serializers for models (and any nested sub-models) that can get applied recursively. HOT 6
- Support for disabling/removing a validator in an inherited model HOT 5
- Context is none even if provided HOT 1
- Simple method to parse models from files HOT 1
- TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard' HOT 14
- `IPv4Network` is marked as incompatible to `IPvAnyNetwork` HOT 1
- `RootModel` from `dataclass` does not preserve extra fields HOT 1
- langchain packages compatibility of pydantic HOT 1
- Override model_config map in child class HOT 1
- create_model; with complex Field definition HOT 1
- New `.v1` import namespace yields different `SettingsError` on Python v3.12 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pydantic.