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

228
Views
Is it possible to modify Pydantic BaseModel attributes just after creating it?

I am starting to learn FastAPI and Pydantic and have a doubt. I have the following subclass of BaseModel

class Product(BaseModel):
  image: str
  name: str

After saving this model, I want image to store the value /static/ + image so as to create nice hyperlinked REST endpoint. This is possible using __post_init_post_parse__ hook of pydantic dataclass but since FastAPI currently doesn't support it, I was wondering what can be a workaround this.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could use a custom validator:

>>> from pydantic import BaseModel, validator
>>> class Product(BaseModel):
    image: str
    name: str
    @validator('image')
    def static_mage(cls, image):
        return '/static/{}'.format(image)


>>> p = Product(image='pic.png', name='product_1')
>>> p
Product(image='/static/pic.png', name='product_1')
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!