from pydantic import BaseModel, Field
class Info(BaseModel):
first_name: str = Field(None)
last_name: int = Field(None)
name = last_name
How can I create a new field with a value of an existing field in the Model?
from pydantic import BaseModel, Field
class Info(BaseModel):
first_name: str = Field(None)
last_name: int = Field(None)
name = last_name
How can I create a new field with a value of an existing field in the Model?
It looks like you can use the post_init
option from attrs
:
from attrs import define, field
@define
class Person:
name: str
lastname: str = field(init=False)
def __attrs_post_init__(self):
self.lastname = self.name + "mr"
p = Person("oren")
print(p)
I'm not sure why you declare last_nale as an int, but I believe it is a mistake.
I recommend to use two ways to do this:
First way (simplest): use @property
from pydantic import BaseModel, Field
class Info(BaseModel):
first_name: str = Field(..., description="First name")
last_name: str = Field(..., description="Last name")
@property
def name(self):
return f"{self.first_name} {self.last_name}"
Output:
>>> person = Info(first_name="Joe", last_name="Doe")
>>> person.name
'Joe Doe'
This is the simplest, but in some cases is not enough. Because:
>>> person.json()
'{"first_name": "Joe", "last_name": "Doe"}'
The second option is using the root_validator
from typing import Optional
from pydantic import BaseModel, Field, root_validator
class Info(BaseModel):
first_name: str = Field(..., description="First name")
last_name: str = Field(..., description="Last name")
name: Optional[str] = None
@root_validator
def set_name(cls, values):
first_name = values.get("first_name")
last_name = values.get("last_name")
values["name"] = f"{first_name} {last_name}"
return values
class Config:
validate_assignment = True # will provide update name after changing other values
Output:
>>> person = Info(first_name="Joe", last_name="Doe")
>>> person.name
'Joe Doe'
>>> person.json()
'{"first_name": "Joe", "last_name": "Doe", "name": "Joe Doe"}'