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

174
Visualizações
Pydantic - Use schema field as field type in another schema

Is this concept somehow possible in Pydantic:

## one file
class StatusOut(BaseModel):
    id: int
    name: str


## another file
class UserOut(BaseModel):
    name: str
    status_name: StatusOut.name  ## IS THIS ACHIEVABLE???

I get an error "AttributeError: type object 'StatusOut' has no attribute 'name'"

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

So the first and only thing I got in mind is to override validation with @validator(...) decorator.

from pydantic import BaseModel, validator


class StatusOut(BaseModel):
    id: int
    name: str


class UserOut(BaseModel):
    name: str
    status_name: StatusOut

    @validator("status_name")
    def redefine_status_name(cls, v):
        return v.name


status = StatusOut(id=1, name="status")

user = UserOut(name="test", status_name=status)

print(user.json())  # {"name": "test", "status_name": "status"}

I guess there is no need to explain what I did, I just returned StatusOut().name instead of StatusOut itself

Although I do not suggest doing that, as it makes status_name's real type not the one that is declared and might confuse you in future

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