Skip to content
Discussion options

You must be logged in to vote

This is a good question!

My suspicion is that there isn't a solution that works in full generality with full type-safety and uses less code than just creating separate models for each usage pattern.

You can get some reuse via inheritance:

class Profile(BaseModel):
    first_name: str
    last_name: str
    title: str = None
    age: int = None
    tagline: str = None

class ProfilePatch(Profile):
    first_name: str = None
    last_name: str = None

This is typically what I do to handle this "problem" in my projects.


I don't think it would be too hard to make a patch-model-generating function through the use of pydantic.create_model that takes an input model, and returns a new model that …

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by tiangolo
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem question-migrate
3 participants
Converted from issue

This discussion was converted from issue #600 on February 28, 2023 11:29.