Skip to content

How to include or exclude fields for list of nested models? #9382

Closed Answered by AlessandroMiola
SDAravind asked this question in Question
Discussion options

You must be logged in to vote

Hi @SDAravind,
the following should do the trick:

bar.model_dump(include={"c": True, "d": {"b"}, "e": {"__all__": {"a"}}})

Quoting the docs

Special care must be taken when including or excluding fields from a list or tuple of submodels or dictionaries. In this scenario, model_dump and related methods expect integer keys for element-wise inclusion or exclusion. To exclude (ed. or include) a field from every member of a list or tuple, the dictionary key '__all__' can be used, as shown here

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@SDAravind
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by SDAravind
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants