I have a request from grafana, and the body is json like as below.
```{
"xhrStatus": "complete",
"request": {
"method": "POST",
"url": "api/datasources/proxy/3/query",
"data": {
"app": "dashboard",
"requestId": "Q107",
"timezone": "browser",
"panelId": 18,
"dashboardId": 2,
"range": {
"from": "2020-02-14T07:30:04.534Z",
"to": "2020-02-14T07:51:17.268Z",
"raw": {
"from": "2020-02-14T07:30:04.534Z",
"to": "2020-02-14T07:51:17.268Z"
}
},
"timeInfo": "",
"interval": "1s",
"intervalMs": 1000,
"targets": [
{
"data": null,
"target": "responseCode2xx",
"refId": "A",
"hide": false,
"type": "timeseries"
}
],
"maxDataPoints": 1408,
"scopedVars": {
"instance_id": {
"text": "${VAR_INSTANCE_ID}",
"value": "${VAR_INSTANCE_ID}"
},
"__from": {
"text": "1581665404534",
"value": "1581665404534"
},
"__to": {
"text": "1581666677268",
"value": "1581666677268"
},
"__interval": {
"text": "1s",
"value": "1s"
},
"__interval_ms": {
"text": "1000",
"value": 1000
}
},
"cacheTimeout": null,
"startTime": 1583045309474,
"rangeRaw": {
"from": "2020-02-14T07:30:04.534Z",
"to": "2020-02-14T07:51:17.268Z"
},
"adhocFilters": [
{
"condition": "",
"key": "instance_id",
"operator": "=",
"value": "02111"
}
]
},
"withCredentials": false
}
}
as you can see, when I model this json, I have to use "from" keyword which is already reserved keyword.
class from_to(BaseModel):
from: str = None
to : str = None
```
I modelled like this. However, when I launch the process, I got syntax error. So, I want to know how can I use the reserved keyword as request body? I cannot change the body reqeust keyword because request is from the grafana json tool which is out of my control.
thanks!
This is more of a pydantic question. Have you tried alias?
@phy25
Thank you for your answering!
I didn't know alias, but it doesn't seem to fit in my case.
Alias could be used like below, but it cannot solve the syntax error issue from using "from" keyword
class from_to(BaseModel):
from: str = Field(None, alias='Gender')
to : str = None
from fastapi import FastAPI
from pydantic import BaseModel, Field
class Item(BaseModel):
time_from: str = Field(None, alias='from')
app = FastAPI()
@app.post("/")
async def create_item(item: Item):
return item
Thanks for the help here @phy25 ! :bowing_man: :cake:
Thanks @jsrimr for reporting back and closing the issue :+1:
Most helpful comment