File: many_to_one.py

package info (click to toggle)
python-odmantic 1.0.2-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,640 kB
  • sloc: python: 8,547; sh: 37; makefile: 34; xml: 13; javascript: 3
file content (26 lines) | stat: -rw-r--r-- 634 bytes parent folder | download | duplicates (2)
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
from odmantic import AIOEngine, Model, Reference


class Publisher(Model):
    name: str
    founded: int
    location: str


class Book(Model):
    title: str
    pages: int
    publisher: Publisher = Reference()


hachette = Publisher(name="Hachette Livre", founded=1826, location="FR")
harper = Publisher(name="HarperCollins", founded=1989, location="US")

books = [
    Book(title="They Didn't See Us Coming", pages=304, publisher=hachette),
    Book(title="This Isn't Happening", pages=256, publisher=hachette),
    Book(title="Prodigal Summer", pages=464, publisher=harper),
]

engine = AIOEngine()
await engine.save_all(books)