Skip to content

Instantly share code, notes, and snippets.

@GGontijo
Created October 24, 2024 14:49
Show Gist options
  • Save GGontijo/e239acdeb587e61f779b3edbfdfb8aba to your computer and use it in GitHub Desktop.
Save GGontijo/e239acdeb587e61f779b3edbfdfb8aba to your computer and use it in GitHub Desktop.
flatten pydantic models
class Ipva(BaseModel):
veiculo: Veiculo
status: str = Field(alias='STATUS')
ano: Optional[str] = Field(None, alias='ANO')
valor: Optional[str] = Field(None, alias='VALOR')
juros: Optional[str] = Field(None, alias='JUROS')
multa: Optional[str] = Field(None, alias='MULTA')
correcao: Optional[str] = Field(None, alias='CORRECAO')
tse: Optional[str] = Field(None, alias='TSE')
desconto: Optional[str] = Field(None, alias='DESCONTO')
total: Optional[str] = Field(None, alias='TOTAL')
parcelaspagas: Optional[str] = Field(None, alias='PARCELAS_PAGAS')
totalparcelas: Optional[str] = Field(None, alias='TOTAL_PARCELAS')
situacao: Optional[str] = Field(None, alias='SITUACAO')
def flatten(self, use_alias_as_key: bool = False) -> dict:
'Retorna o modelo em formato de dicionário achatando os dados do veículo'
ipva_data = self.model_dump(by_alias=use_alias_as_key)
veiculo_data = ipva_data.pop('veiculo')
ipva_data.update(veiculo_data)
return ipva_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment