Pydantic:如何使用一个字段的值来设置其他字段的值?
Pydantic: How to use one field's value to set values for other fields?
我有什么
字典:
user_dict = {'user': {'field1': 'value1',
'field2': 'value2'},
'admin':{'field1': 'value3',
'field2': 'value4'}}
Pydantic 模型:
class User(BaseModel):
account_type: Optional[str] = 'user'
field1: Optional[str] = ''
field1: Optional[str] = ''
class Config:
validate_assignment = True
@validator("account_type", pre=True, always=True)
def _set_account_type(cls, account_type: str):
return account_type or "user"
期望的结果
我想根据 account_type 从 user_dict 中查找 field1 和 field2 的值。所以我应该能够做这样的事情:
user = User()
print(user.field1)
# > 'value1'
print(user.field2)
# > 'value2'
user2 = User(field1=None, field2=None)
print(user2.field1)
# > 'value1'
print(user2.field2)
# > 'value2'
user3 = User(account_type="admin")
print(user3.field1)
# > 'value3'
print(user3.field2)
# > 'value4'
我试过的
class User(BaseModel):
account_type: Optional[str] = 'user'
field1: Optional[str] = user_dict['account_type']['field1']
field1: Optional[str] = user_dict['account_type']['field2']
class Config:
validate_assignment = True
@validator("account_type", pre=True, always=True)
def _set_account_type(cls, account_type: str):
return account_type or "user"
user = User()
print(user.field1)
# > 'value1'
user = User(field1=None)
print(user.field1)
# > None
对 field1 和 field2 使用验证器:
class User(BaseModel):
account_type: Optional[str] = 'user'
field1: Optional[str]
field1: Optional[str]
class Config:
validate_assignment = True
@validator("account_type", pre=True, always=True)
def _set_account_type(cls, account_type: str):
return account_type or "user"
@validator("field1", pre=True, always=True)
def _set_plan_credits(cls, value):
if value is None:
value = 'value1'
return user_dict['account_type']['field1'] or value
@validator("field2", pre=True, always=True)
def _set_rollover_credits(cls, value):
if value is None:
value = 'value2'
return user_dict['account_type']['field2'] or value
user = User()
# > AttributeError: type object 'Account' has no attribute 'account_type'
关于如何获得预期结果有什么想法吗?
我使用 root_validator
装饰器解决了这个问题,如下所示:
解法:
@root_validator(pre=False)
def _set_fields(cls, values: dict) -> dict:
"""This is a validator that sets the field values based on the
the user's account type.
Args:
values (dict): Stores the attributes of the User object.
Returns:
dict: The attributes of the user object with the user's fields.
"""
values["field1"] = user_dict[values["account_type"]]["field1"]
values["field2"] = user_dict[values["account_type"]]["field2"]
return values
我有什么
字典:
user_dict = {'user': {'field1': 'value1',
'field2': 'value2'},
'admin':{'field1': 'value3',
'field2': 'value4'}}
Pydantic 模型:
class User(BaseModel):
account_type: Optional[str] = 'user'
field1: Optional[str] = ''
field1: Optional[str] = ''
class Config:
validate_assignment = True
@validator("account_type", pre=True, always=True)
def _set_account_type(cls, account_type: str):
return account_type or "user"
期望的结果
我想根据 account_type 从 user_dict 中查找 field1 和 field2 的值。所以我应该能够做这样的事情:
user = User()
print(user.field1)
# > 'value1'
print(user.field2)
# > 'value2'
user2 = User(field1=None, field2=None)
print(user2.field1)
# > 'value1'
print(user2.field2)
# > 'value2'
user3 = User(account_type="admin")
print(user3.field1)
# > 'value3'
print(user3.field2)
# > 'value4'
我试过的
class User(BaseModel):
account_type: Optional[str] = 'user'
field1: Optional[str] = user_dict['account_type']['field1']
field1: Optional[str] = user_dict['account_type']['field2']
class Config:
validate_assignment = True
@validator("account_type", pre=True, always=True)
def _set_account_type(cls, account_type: str):
return account_type or "user"
user = User()
print(user.field1)
# > 'value1'
user = User(field1=None)
print(user.field1)
# > None
对 field1 和 field2 使用验证器:
class User(BaseModel):
account_type: Optional[str] = 'user'
field1: Optional[str]
field1: Optional[str]
class Config:
validate_assignment = True
@validator("account_type", pre=True, always=True)
def _set_account_type(cls, account_type: str):
return account_type or "user"
@validator("field1", pre=True, always=True)
def _set_plan_credits(cls, value):
if value is None:
value = 'value1'
return user_dict['account_type']['field1'] or value
@validator("field2", pre=True, always=True)
def _set_rollover_credits(cls, value):
if value is None:
value = 'value2'
return user_dict['account_type']['field2'] or value
user = User()
# > AttributeError: type object 'Account' has no attribute 'account_type'
关于如何获得预期结果有什么想法吗?
我使用 root_validator
装饰器解决了这个问题,如下所示:
解法:
@root_validator(pre=False)
def _set_fields(cls, values: dict) -> dict:
"""This is a validator that sets the field values based on the
the user's account type.
Args:
values (dict): Stores the attributes of the User object.
Returns:
dict: The attributes of the user object with the user's fields.
"""
values["field1"] = user_dict[values["account_type"]]["field1"]
values["field2"] = user_dict[values["account_type"]]["field2"]
return values