Pydantic: How do I use a keyword field name?

Viewed 3147

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.

1 Answers

Use an alias:

class MyRequest(BaseModel):
    foo: str
    abc: int
    from_field: int = Field(..., alias='from')
Related