Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

396
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda