I have two pydantic classes like this.
class Parent(BaseModel):
id: int
name: str
email: str
class ParentUpdate(BaseModel):
id: Optional[int]
name: Optional[str]
email: Optional[str]
Both of these are practically the same but the Parent
class makes all fields required.
I want to use the Parent
class for POST request body in FastAPI, hence all fields should be required. But I want to use the latter for PUT request body since the user can set selective fields and the remaining stays the same.
I have taken a look at Required Optional Fields but they do not correspond to what I want to do.
If there was a way I could inherit the Parent
class in ParentUpdate
and modified all the fields in Parent
to make them Optional
that would reduce the clutter. Additionally, there are some validators present in the Parent
class which I have to rewrite in the ParentUpdate
class which I also want to avoid.
Is there any way of doing this?