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
Feature: DTOData.create_instance()
nested kwargs
#1741
Conversation
This commit modifies the way that DTO backends manage their `TransferFieldDefinition` instances. Previously, the backend would not keep a field definition for excluded fields, however, this proved limiting when wanting to use the `factory._backends.utils.transfer_instance_data()` method to move data from unstructured builtins to data model instances. In this scenario, and generally whenever we are moving inbound data from a transfer model to the data model, we need to transfer a value for all fields, including fields that are excluded from request and response data, unless an omitted field has a default value. Adds an `is_excluded` attribute to `TransferFieldDefinition` so that generated transfer models can still omit the field, and so that the field can still be omitted from return data. We no longer modify the `TransferFieldDefinition` type and defaults in the case of `is_partial`. Instead, the backend implementations do the `UnsetType` splicing at transfer model generation time. This is important so that we can still discern when a field definition originally had a default value specified, after the partial handling has been applied.
9a3b262
to
9198419
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! A section about this feature in the docs would be nice I think.
Cheers! Yes, I will add to the tutorial and a How To section in usage docs before I close out. |
b662312
to
670f5f6
Compare
Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/1741 |
This exercises the partial path of the Pydantic backend.
Kudos, SonarCloud Quality Gate passed! |
DTOData.create_instance()
nested kwargs
This PR adds a feature to the
DTOData.create_instance()
method that supports providing values for arbitrarily nested data via kwargs using a double-underscore syntax. For exampledata.create_instance(foo__bar="baz")
.In the following example, the DTO is configured to exclude the
baz
attribute from request data, but the field has no default, and so is required in order to be able to instantiate the hierarchy of models. The kwargfoo__baz="world"
is passed tocreate_instance()
call, which givesBar.foo.baz
a value.Closes #1727