Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

400
Vistas
How to convert dictionary to schema in FastAPI

The below schema can be converted to dict using branch.__dict__

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

How can i convert the dict object to schema again in FastAPI

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can use double asterisk ** for unpacking the multiple variables to turn to a single object

class Branch(BaseModel):
    name: str
    regionID: str


def check_type(obj):
    return f"{obj}  \n  type: {type(obj)}"

I created this class and type checker, then i created the branch object

branch = Branch(name='jfslkjf', regionID='fdfasd')
check_type(branch)

Out: name='jfslkjf' regionID='fdfasd'  
     type: <class '__main__.Branch'>

Then i converted to a dict

branch_dict = branch.__dict__
check_type(branch_dict)


Out: {'name': 'jfslkjf', 'regionID': 'fdfasd'}  
     type: <class 'dict'>

So on i used double asteriks to unpack it

test_branch = Branch(**branch_dict)
check_type(test_branch)

Out: name='jfslkjf' regionID='fdfasd'  
     type: <class '__main__.Branch'>
over 4 years ago · Santiago Trujillo Denunciar

0

You can simply spread the dict back into the Branch.

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

Fastapi uses pydantic.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda