Initial commit
This commit is contained in:
4
training/demos/09-sqlite/orm-peewee/README.md
Normal file
4
training/demos/09-sqlite/orm-peewee/README.md
Normal file
@ -0,0 +1,4 @@
|
||||
# Simple ORM demo with Peewee
|
||||
|
||||
Peewee simple definition of a model and simple code to
|
||||
take advantage of the tool.
|
1
training/demos/09-sqlite/orm-peewee/requirements.txt
Normal file
1
training/demos/09-sqlite/orm-peewee/requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
peewee
|
Binary file not shown.
27
training/demos/09-sqlite/orm-peewee/source/demo_app.py
Normal file
27
training/demos/09-sqlite/orm-peewee/source/demo_app.py
Normal file
@ -0,0 +1,27 @@
|
||||
"""
|
||||
Simple demo application to showcase Peewee.
|
||||
|
||||
Simple script with model definition to showcase the
|
||||
use of ORMs and Peewee for beginners in database usage.
|
||||
|
||||
"""
|
||||
import peewee as pw # pip install peewee
|
||||
from models import Person
|
||||
|
||||
|
||||
if __name__ == "__main__": # Only True if current script is executed manually.
|
||||
# Initialize a Peewee database for SQLite
|
||||
db = pw.SqliteDatabase("database/demo-file.sqlite3")
|
||||
# Make the Person model use the database.
|
||||
Person.bind(db)
|
||||
# Query the `person` table.
|
||||
query = Person.select().where(Person.age > 50)
|
||||
|
||||
# Display the results with a friendly formatting.
|
||||
print(f"List of people older than 50 years old:\n{'–' * 80}")
|
||||
for person in query: # type: Person
|
||||
print(f"{person.id:02d}. {person.get_full_name():<30}: {person.age} years old.")
|
||||
|
||||
# Exemple pour créer un nouvel objet Person dans ma table
|
||||
# nouvelle_personne = Person(first_name="Bob", last_name="Bob", age=90)
|
||||
# nouvelle_personne.save()
|
1
training/demos/09-sqlite/orm-peewee/source/demo_test.py
Normal file
1
training/demos/09-sqlite/orm-peewee/source/demo_test.py
Normal file
@ -0,0 +1 @@
|
||||
import demo_app
|
@ -0,0 +1,3 @@
|
||||
from .person import Person
|
||||
|
||||
__all__ = ["Person"]
|
32
training/demos/09-sqlite/orm-peewee/source/models/person.py
Normal file
32
training/demos/09-sqlite/orm-peewee/source/models/person.py
Normal file
@ -0,0 +1,32 @@
|
||||
import peewee as pw
|
||||
|
||||
|
||||
class Person(pw.Model):
|
||||
"""
|
||||
Model class for the Person table.
|
||||
|
||||
Simple representation as a class of the Person
|
||||
table columns. Each object of a Person can be persisted or
|
||||
retrieved from the database.
|
||||
|
||||
Attributes:
|
||||
Person.id: identifier column. Primary key.
|
||||
Person.last_name: N/A
|
||||
Person.first_name: N/A
|
||||
Person.age: Age of the person in years.
|
||||
|
||||
"""
|
||||
id = pw.BigAutoField(primary_key=True)
|
||||
last_name = pw.CharField(column_name="nom", max_length=30, null=False)
|
||||
first_name = pw.CharField(column_name="prenom", max_length=20, null=False)
|
||||
age = pw.IntegerField(null=False)
|
||||
|
||||
class Meta:
|
||||
indexes = (
|
||||
# Make a UNIQUE index with the two following columns
|
||||
(("last_name", "first_name"), True),
|
||||
)
|
||||
|
||||
def get_full_name(self) -> str:
|
||||
"""Get the full name of the person."""
|
||||
return f"{self.first_name} {self.last_name}"
|
Reference in New Issue
Block a user