Python 如何在FastAPI中将字典转换为模式
可以使用Python 如何在FastAPI中将字典转换为模式,python,schema,fastapi,pydantic,Python,Schema,Fastapi,Pydantic,可以使用分支将下面的架构转换为dict branch = BranchIn(name='jfslkjf', regionId='fdfasd') 如何在FastAPI中将dict对象再次转换为模式您可以使用双星号**将多个变量解包为单个对象 class Branch(BaseModel): name: str regionID: str def check_type(obj): return f"{obj} \n type: {type(obj)}&q
分支将下面的架构转换为dict
branch = BranchIn(name='jfslkjf', regionId='fdfasd')
如何在FastAPI中将dict对象再次转换为模式您可以使用双星号**
将多个变量解包为单个对象
class Branch(BaseModel):
name: str
regionID: str
def check_type(obj):
return f"{obj} \n type: {type(obj)}"
我创建了这个类和类型检查器,然后创建了分支对象
branch = Branch(name='jfslkjf', regionID='fdfasd')
check_type(branch)
Out: name='jfslkjf' regionID='fdfasd'
type: <class '__main__.Branch'>
branch=branch(name='jfslkjf',regionID='fdfasd')
检查类型(分支)
输出:name='jfslkjf'regionID='fdfasd'
类型:
然后我改成了口述
branch_dict = branch.__dict__
check_type(branch_dict)
Out: {'name': 'jfslkjf', 'regionID': 'fdfasd'}
type: <class 'dict'>
branch\u dict=分支__
检查类型(分支目录)
输出:{'name':'jfslkjf','regionID':'fdfasd'}
类型:
所以我用了两个星号来打开它
test_branch = Branch(**branch_dict)
check_type(test_branch)
Out: name='jfslkjf' regionID='fdfasd'
type: <class '__main__.Branch'>
test\u branch=branch(**branch\u dict)
检查类型(测试分支)
输出:name='jfslkjf'regionID='fdfasd'
类型:
您可以使用双星号***
将多个变量解包为单个对象
class Branch(BaseModel):
name: str
regionID: str
def check_type(obj):
return f"{obj} \n type: {type(obj)}"
我创建了这个类和类型检查器,然后创建了分支对象
branch = Branch(name='jfslkjf', regionID='fdfasd')
check_type(branch)
Out: name='jfslkjf' regionID='fdfasd'
type: <class '__main__.Branch'>
branch=branch(name='jfslkjf',regionID='fdfasd')
检查类型(分支)
输出:name='jfslkjf'regionID='fdfasd'
类型:
然后我改成了口述
branch_dict = branch.__dict__
check_type(branch_dict)
Out: {'name': 'jfslkjf', 'regionID': 'fdfasd'}
type: <class 'dict'>
branch\u dict=分支__
检查类型(分支目录)
输出:{'name':'jfslkjf','regionID':'fdfasd'}
类型:
所以我用了两个星号来打开它
test_branch = Branch(**branch_dict)
check_type(test_branch)
Out: name='jfslkjf' regionID='fdfasd'
type: <class '__main__.Branch'>
test\u branch=branch(**branch\u dict)
检查类型(测试分支)
输出:name='jfslkjf'regionID='fdfasd'
类型:
您可以简单地将dict传回分支
branchDict={'name':'jfslkjf','regionId':'fdfasd}
branchObj=分支(**branchDict)
Fastapi使用pydantic。您只需将dict重新展开到分支中即可
branchDict={'name':'jfslkjf','regionId':'fdfasd}
branchObj=分支(**branchDict)
Fastapi使用pydantic。将dict转换为模式是什么意思。与上述操作相反…将dict转换为模式是什么意思。与上述操作相反。。。