I have 2 classes:
class UserCreate(BaseModel):
avatar: HttpUrl = Field(..., description="Avatar", example="https://picsum.photos/200")
name: str = Field(..., max_length=20, description="A single word", example='Ivan')
birthdate: datetime_date = Field(..., description="Two digits", example='1980-1-1')
comment: Optional[str] = Field(..., max_length=512, description="lorem ipsum about a user", example='blah blah')
and I want to create a UserUpdate
class that will inherit every field from the parent class and make it Optional
.
a Result class must look like:
class UserUpdate(BaseModel):
avatar: typing.Optional[HttpUrl] = Field(..., description="Avatar", example="https://picsum.photos/200")
name: typing.Optional[str] = Field(..., max_length=20, description="A single word", example='Ivan')
birthdate: typing.Optional[datetime_date] = Field(..., description="Two digits", example='1980-1-1')
comment: typing.Optional[str] = Field(..., max_length=512, description="lorem ipsum about a user", example='blah blah')
But obviously, I want to make it automatically, like:
class UserUpdate(UserCreate):
def foo(fields_from_user_create):
for fields in fields_from_user_create:
field = typing.Optional(field)
question from:
https://stackoverflow.com/questions/65836195/python-pydantic-make-every-field-of-ancestor-are-optional 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…