-
-
Notifications
You must be signed in to change notification settings - Fork 331
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
Enhancement: Provide option to set DTOs for nested fields #3500
Comments
What about supplying config objects, instead of full-blown DTOs? config = DTOConfig(
exclude={
"id",
},
field_dto={
"address": user_address_config,
"pets": user_pets_config,
}
)
ReadUserDTO = SQLAlchemyDTO[Annotated[User, config]] |
Here's some questions:
|
That... are all good questions :D so, using configs instead of full DTO makes sense to me - in the end, the root DTO already takes care of processing any nested models it's just about sharing configs for nested models in an easy way. For the other things it should be one general approach that is consistent - in my opinion: Parent overules child. The parent structure is used for handling communication, so it owns the interface description and should have last say in renaming and nesting and so on. I'm not sure if I understand the last question: With |
Yeah, so it would just read the exclude, include, and any explicit field renames from the nested config objects.
|
Ah okay, so yea then for partial I'd also say parent overrules child config. |
So I think how this would work then, is inside |
I have an implementation here #2465 that addresses this. I've tried it in production as well. My main use case is having a SQLAlchemy model in a dataclass. The only thing missing in that implementation is writing tests. |
Summary
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.
Basic Example
Based on the example in the docs:
New
Currently
Drawbacks and Impact
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.
Unresolved questions
No response
Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh dashboard
The text was updated successfully, but these errors were encountered: