Debian Codegraph @

docs_src/dataclasses_/tutorial003_py310.py

raw
1 from dataclasses import field # (1)
2
3 from fastapi import FastAPI
4 from pydantic.dataclasses import dataclass # (2)
5
6
7 @dataclass
8 class Item:
9 name: str
10 description: str | None = None
11
12
13 @dataclass
14 class Author:
15 name: str
16 items: list[Item] = field(default_factory=list) # (3)
17
18
19 app = FastAPI()
20
21
22 @app.post("/authors/{author_id}/items/", response_model=Author) # (4)
23 async def create_author_items(author_id: str, items: list[Item]): # (5)
24 return {"name": author_id, "items": items} # (6)
25
26
27 @app.get("/authors/", response_model=list[Author]) # (7)
28 def get_authors(): # (8)
29 return [ # (9)
30 {
31 "name": "Breaters",
32 "items": [
33 {
34 "name": "Island In The Moon",
35 "description": "A place to be playin' and havin' fun",
36 },
37 {"name": "Holy Buddies"},
38 ],
39 },
40 {
41 "name": "System of an Up",
42 "items": [
43 {
44 "name": "Salt",
45 "description": "The kombucha mushroom people's favorite",
46 },
47 {"name": "Pad Thai"},
48 {
49 "name": "Lonely Night",
50 "description": "The mostests lonliest nightiest of allest",
51 },
52 ],
53 },
54 ]