Python Pydantic use multiple field alias

Viewed 711

Is there any way to use multiple field alias without using root_validator?

My current code is this:

class GetInfo(BaseModel):
    name: str = Field(alias='name1')
    name2: str = Field(alias='name2')
    
    @root_validator
    def format_name(cls, values):
        if values['name'] is None:
            if values['name2'] is not None:
                values['name'] = values['name2']
            return values
        return None

The name1 field is sometimes empty while the name2 field is mostly not empty I wanted to use the name2 if the name1 is really empty.

Or can you suggest better way than what I did?

0 Answers
Related