How to include or exclude fields for list of nested models? #9382
-
I would like to include or exclude on list of nested models. How would I do that with below example? Please point me in right direction that would help me solve this. from pydantic import BaseModel
class Foo(BaseModel):
a: str
b: int
class Bar(BaseModel):
c: str
d: Foo
e: list[Foo]
Bar(
c="test", d=Foo(a="foo", b=10), e=[Foo(a="test1", b=1), Foo(a="test2", b=2)]
).model_dump(include={"c": True, "d": {"b"}, "e": {"a"}})
)
>>> {'c': 'test', 'd': {'b': 10}, 'e': []} Expected Output: I would like see below output >>> {'c': 'test', 'd': {'b': 10}, 'e': [{'a': 'test1'}, {'a': 'test2'}]} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Does #6699 provide any answers? |
Beta Was this translation helpful? Give feedback.
-
Hi @SDAravind,
Quoting the docs
|
Beta Was this translation helpful? Give feedback.
Hi @SDAravind,
the following should do the trick:
Quoting the docs