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

241
Views
How to use a reserved keyword in pydantic model

I need to create a schema but it has a column called global, and when I try to write this, I got an error.

class User(BaseModel):

    id:int
    global:bool

I try to use another name, but gives another error when try to save in db.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It looks like you are using the pydantic module. You can't use the name global because it's a reserved keyword so you need to use this trick to convert it.

class User(BaseModel):
    id: int
    global_: bool

    class Config:
        fields = {
            'global_': 'global'
        }

or

class User(BaseModel):
    id: int
    global_: bool = Field(..., alias='global')

To create a class you have to use a dictionary (because User(id=1, global=False) also throws an error:

user = User(**{'id': 1, 'global': False})

To get data in correct schema use by_alias:

user.dict(by_alias=True)
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!