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.
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)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments