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

173
Vistas
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 Respuestas
Responde la pregunta

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 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