Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

395
Views
Cómo convertir diccionario a esquema en FastAPI

El siguiente esquema se puede convertir a dict usando branch.__dict__

 branch = BranchIn(name='jfslkjf', regionId='fdfasd')
 branchDict = branch.__dict__ branchDict = {'name': 'jfslkjf', 'regionId': 'fdfasd' }

¿Cómo puedo volver a convertir el objeto dict en un esquema en FastAPI?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puede usar doble asterisco ** para desempaquetar las múltiples variables para convertirlas en un solo objeto

 class Branch(BaseModel): name: str regionID: str def check_type(obj): return f"{obj} \n type: {type(obj)}"

Creé esta clase y tipo de verificador, luego creé el objeto de rama

 branch = Branch(name='jfslkjf', regionID='fdfasd') check_type(branch) Out: name='jfslkjf' regionID='fdfasd' type: <class '__main__.Branch'>

Luego lo convertí a un dict

 branch_dict = branch.__dict__ check_type(branch_dict) Out: {'name': 'jfslkjf', 'regionID': 'fdfasd'} type: <class 'dict'>

Entonces usé asteriscos dobles para descomprimirlo

 test_branch = Branch(**branch_dict) check_type(test_branch) Out: name='jfslkjf' regionID='fdfasd' type: <class '__main__.Branch'>
over 4 years ago · Santiago Trujillo Report

0

Simplemente puede difundir el dict de nuevo en la Branch .

 branchDict = {'name': 'jfslkjf', 'regionId': 'fdfasd' } branchObj = Branch(**branchDict)

Fastapi usa pydantic.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!