Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: DTO tutorial should mention how to exclude from collection of nested union models #3978

Open
HorusTheSonOfOsiris opened this issue Jan 28, 2025 · 0 comments
Labels
Documentation 📚 This is related to documentation

Comments

@HorusTheSonOfOsiris
Copy link

Summary

I was going through the documentation on this it mentions how to exclude the fields from a collection of nested model. However, it needs an update where it mentions how to exclude fields if someone uses a collection of nested union models.

For e.g.

# Assuming correct imports are in place

@dataclass
class Address:
    street: str
    city: str
    country: str


@dataclass
class Person:
    name: str
    age: int
    email: str
    address: Address
    children: list['Person' | None]

# The DTO will change like:

class ReadDTO(DataclassDTO[Person]):
    config = DTOConfig(exclude={"email", "address.street", "children.0.0.email", "children.0.0.address"})

# We need to provide two zeroes instead of one.

Now there is a line in the document which states

Given a generic type, with an arbitrary number of type parameters (e.g., GenericType[Type0, Type1, ..., TypeN]), we use the index of the type parameter to indicate which type the exclusion should refer to. For example, a.0.b, excludes the b field from the first type parameter of a, a.1.b excludes the b field from the second type parameter of a, and so on.

However, it is not very clear and an example in the document might help.

Working without union (as per example)

Image

Union introduced

Image

Union introduced (add extra zero to exclude)

Image

Important

Order of union matters too and therefore key must be changed based on the order.

@HorusTheSonOfOsiris HorusTheSonOfOsiris added the Documentation 📚 This is related to documentation label Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation 📚 This is related to documentation
Projects
None yet
Development

No branches or pull requests

1 participant