Comments (7)
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]]
from litestar.
Here's some questions:
- what should happen if the
rename_strategy
for the nested model is different to the parent model config? - what about
max_nested_depth
? If the parent config hasmax_nested_depth=1
and the nested model hasmax_nested_depth=1
, should a nested field of the nested model be included? Or, should the parent nested depth win? partial
, would you expect a non-partial parent model should be allowed to contain a partial inner model?
from litestar.
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 partial
you mean excluding fields? If so, then I'd say that is the point of having nested configs, no? To have a cleaner way of excluding fields per model instead of digging through all nested models from the parent DTO.
from litestar.
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.
Yeah, so it would just read the exclude, include, and any explicit field renames from the nested config objects.
I'm not sure if I understand the last question: With
partial
you mean excluding fields? If so, then I'd say that is the point of having nested configs, no? To have a cleaner way of excluding fields per model instead of digging through all nested models from the parent DTO.
partial
makes the set of all fields not required for a model, basically for PATCH route handling.
from litestar.
Ah okay, so yea then for partial I'd also say parent overrules child config.
from litestar.
So I think how this would work then, is inside DTOConfig.__post_init__()
, we'd take any of the nested config objects and build the dotted path include/exclude/renames into the parent configs include/exclude/rename fields. E.g., if parent had exclude={"foo"}
, and the nested was {"bar": DTOConfig(exclude={"baz"})}
, then the parent exclude would end up as {"foo", "bar.baz"}
after __post_init__()
has executed.
from litestar.
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.
from litestar.
Related Issues (20)
- DTO backend does not transferring mappings with nested models HOT 10
- Bug: SerializationException when running modeling-and-features demo from docs HOT 3
- Bug: Can't convert sqlalchemy model to pydantic model which is inherited from BaseModel HOT 1
- Enhancement: Add Pydantic's error dictionary to ValidationException's extra dict
- Enhancement: make `SessionMiddleware` return custom types HOT 3
- Bug: OpenAPI docs fail to generate with certain validators and pydantic v1 HOT 4
- Bug: Multi-body response incompatible with LoggingMiddleware HOT 2
- Bug: test failures
- Bug: `return_dto` is silently ignored if return data type does not match DTO definition HOT 1
- Bug: Test error
- Enhancement: add `default_json_serializer` to `__all__` in `litestar.logging.config`
- Docs: Migrating to Litestar from Django HOT 1
- Decoupled, transactional communication of domain events and commands
- Docs: Support for domain driven designs
- Enhancement: Repository pattern interfaces HOT 1
- Enhancement: Kubernetes application health endpoints HOT 1
- Enhancement: Allow status code categories in route handlers responses
- Bug: mounted app path interferes with regular paths HOT 5
- Examples not shown when using DTO HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from litestar.