Skip to content

Commit b29273d

Browse files
committed
Updated model file
1 parent 247e2c6 commit b29273d

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

ch04/todos/model.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from typing import List, Optional
2+
3+
from fastapi import Form
4+
from pydantic import BaseModel
5+
6+
7+
class Todo(BaseModel):
8+
id: Optional[int]
9+
item: str
10+
11+
@classmethod
12+
def as_form(
13+
cls,
14+
item: str = Form(...)
15+
):
16+
return cls(item=item)
17+
18+
class Config:
19+
schema_extra = {
20+
"example": {
21+
"id": 1,
22+
"item": "Example schema!"
23+
}
24+
}
25+
26+
27+
class TodoItem(BaseModel):
28+
item: str
29+
30+
class Config:
31+
schema_extra = {
32+
"example": {
33+
"item": "Read the next chapter of the book"
34+
}
35+
}
36+
37+
38+
class TodoItems(BaseModel):
39+
todos: List[TodoItem]
40+
41+
class Config:
42+
schema_extra = {
43+
"example": {
44+
"todos": [
45+
{
46+
"item": "Example schema 1!"
47+
},
48+
{
49+
"item": "Example schema 2!"
50+
}
51+
]
52+
}
53+
}

0 commit comments

Comments
 (0)