I really enjoy working with DTOs as it simplifies my work greatly.
One weakness I've observed is when it comes to nested models, e.g., from including a relationship. I know it's possible to exclude fields of nested fields as explained in the docs, but I think it would be more convenient if one could provide "nested field DTOs". For example, this could be a dict where field names are mapped to DTOs to use for handling the model.
From user perspective this seems cleaner - but I don't know how easy it is to implement this.
Based on the example in the docs:
...
user_address_config = DTOConfig(
exclude={
"id",
"street",
}
)
UserAddressDTO = SQLAlchemyDTO[Annotated[Address, user_address_config]]
user_pets_config = DTOConfig(
exclude={
"id",
"user_id",
}
)
UserPetsDTO = SQLAlchemyDTO[Annotated[Pets, user_pets_config]]
UserDTO = SQLAlchemyDTO[User]
config = DTOConfig(
exclude={
"id",
},
field_dto={
"address": UserAddressDTO,
"pets": UserPetsDTO
}
)
ReadUserDTO = SQLAlchemyDTO[Annotated[User, config]]
...
...
UserDTO = SQLAlchemyDTO[User]
config = DTOConfig(
exclude={
"id",
"address.id",
"address.street",
"pets.0.id",
"pets.0.user_id",
}
)
ReadUserDTO = SQLAlchemyDTO[Annotated[User, config]]
...
In this specific example, the current way is quicker to write and needs less lines. However, as an application becomes more complex there might be the use case where multiple models have a nested field (e.g., a user from an author reference) and would like to use the same nesting specification. Then, re-usability of a nested DTO would be preferably over managing the same exclude list over multiple DTOs.
No response
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too