-
Notifications
You must be signed in to change notification settings - Fork 8
feat(dto): generate dtos with all fields marked as optional #210
Comments
Hey @gazorby - I've done some thinking along these lines too for In your example, what happens if a user sends through So instead of making every attribute optional, I've thought of defining an UnsetType = type("UnsetType", (), {})
UNSET = UnsetType()
class UserUpdate(BaseModel):
id: uuid.UUID
username: str | UnsetType = UNSET
age: int | UnsetType = UNSET In Also, for Thoughts? |
If a user sends I should have given more context about my use case, but using a custom unset type won't work for me actually, because I use DTOs to generate GraphQL input types, using strawberry. A DTO with optional fields will lead to a GraphQL input type like this: # Only id is required
input UserInput {
id: ID!
username: String
age: Int
} Which is what I want. As As for the name, I agree with |
Actually, I prefer your way design wise, but it does not fit my need well. Also, in my previous example, using |
Maybe we should do up some POCs and look at the conditions under which they work/don't work. Good chance that there isn't a one-size-fits-all solution and we end up with multiple new features to cover these cases. Another thing to consider is the openapi that would be generated for these. |
I'm implementing an handler to update some resource where input only contains optional fields, except for the resource identifier.
There are two things that make implementing this a bit cumbersome:
Update DTO
As we need input fields to be optional only, dto fields need to optional too. Currently, dtos can only be generating for
read
orwrite
purpose, meaning that I need to rewrite a full model for the update.Proposal: A new
update
purpose that generate a dto with all fields marked as optional.to_mapped()
set all attributesCurrently,
to_mapped()
set all attributes from the pydantic instance, included ones with default values. This is good when inserting rows, but may not be desirable when updating them as basically all attributes will be marked as modified by sqlalchemy which will try to update them in database.Proposal: New
exclude_defaults
/exclude_unset
params onto_mapped()
ensuring that only setted attributes from pydantic model are set on sqlalchemy instance.Use case
Use case using the above proposals:
The text was updated successfully, but these errors were encountered: