-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpydantic_main.py
More file actions
49 lines (35 loc) · 822 Bytes
/
pydantic_main.py
File metadata and controls
49 lines (35 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
#creating a model using pydantic
class Item(BaseModel):
id:int
name:str
description:str
price:int
on_offer:bool
#this only return the simple jason response
@app.get("/")
def root():
return {"message": "Hello World"}
#giving item id as query parameter
@app.put("/items/{item_id}")
def update_items(item_id:int,item:Item):
#returing the data from the model
return{
'name':item.name,
'description' : item.description,
'price' : item.price,
'on_offer' : item.on_offer
}
#to give data we have to provide it in its body
#like this
'''
{
"id":1,
"name":"milk",
"description":"1 litre milk ",
"price":2000,
"on_offer":true
}
'''