Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!