Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pydantic: How do I use a keyword field name?

I'm trying to model an API request in Pydantic. I have to model a field called "from". Since "from" is a keyword in python, Pydantic throws an error.

Model

class MyRequest(BaseModel):
    foo: str
    abc: int
    from: int

Error thrown by Pydantic

File "test.py", line 6
    from: int

SyntaxError: invalid syntax

Is there to model this "from" field? Changing the parameter name is not an option.

like image 225
shrshank Avatar asked Mar 10 '20 16:03

shrshank


Video Answer


1 Answers

Use an alias:

class MyRequest(BaseModel):
    foo: str
    abc: int
    from_field: int = Field(..., alias='from')
like image 106
SColvin Avatar answered Sep 23 '22 03:09

SColvin