SoFunction
Updated on 2024-11-16

Python's lightweight ORM framework peewee using tutorials

The most widely used ORM frameworks are SQLAlchemy and Django's own ORM framework, but SQLAlchemy's syntax is obviously relative to Django's ORM framework trouble.

Django itself is a web framework , relatively heavyweight , just to use the functionality of Django's ORM framework , and install Django is a bit lead to system bloat . The peewee framework syntax is almost the same as Django's ORM framework , but very lightweight .

It is very easy to install:

pip install peewee

If you report the following error while using mysql database:

: MySQL driver not installed!

Then you need to install a mysql driver:

pip install pymysql

The whl package for peewee is 880kB and the whl package for pymysql is 51KB, very lightweight.

The official address for peewee's documentation:/en/latest/

Test the features below:

from peewee import *

db = MySQLDatabase('test', host="localhost", user='root', passwd='123456', port=3306)


# Define Person
class Person(Model):
  name = CharField()
  birthday = DateField()
  is_relative = BooleanField()

  class Meta:
    database = db
    
def test_create():
  Person.create_table()
  # Create multiple tables like this
  # database.create_tables([Person])


def test_insert():
  # Add a piece of data
  p = Person(name='Xiaohua', birthday=date(1996, 12, 20), is_relative=True)
  ()


def test_delete():
  # Delete data with name perter
  ().where( == 'perter').execute()
  # Instantiated data, use delete_instance
  p = Person(name='Xiaohua', birthday=date(1996, 12, 20), is_relative=False)
   = 1
  ()
  p.delete_instance()


def test_update():
  # Instantiated data, specify id as primary key, then save is to update the data.
  p = Person(name='Xiaohua', birthday=date(1996, 12, 20), is_relative=False)
   = 1
  ()

  # Update birthday data
  q = ({: date(1983, 12, 21)}).where( == 'Xiaohua')
  ()


def test_query():
  # Query a single piece of data
  p = ( == 'Xiaohua')
  print(, , p.is_relative)

  # Query using where().get()
  p = ().where( == 'Xiaohua').get()
  print(, , p.is_relative)

  # Query multiple pieces of data
  persons = ().where(Person.is_relative == True)
  for p in persons:
    print(, , p.is_relative)

Test an individual method below.

Test creating a table:

if __name__=="__main__":
  Person.create_table()

After execution, check that the database has successfully created the following table:

Test insertion of data:

if __name__=="__main__":
  p = Person(name='Xiaohua', birthday=date(1996, 12, 20), is_relative=True)
  ()

After execution, the table data has an extra row:

Test query data:

if __name__=="__main__":
 p = ( == 'Xiaohua')
  print(, , p.is_relative)

Results:

Siu Wah 1996-12-20 True

Test deletion of data:

if __name__=="__main__":
  ().where( == 'Xiaohua').execute()

After execution, the corresponding record in the database is deleted:

Test the modification data:

if __name__ == "__main__":
  p = Person(name='Little New', birthday=date(1995, 6, 20), is_relative=False)
  ()
  # Update birthday data
  q = ({: date(1983, 5, 21)}).where( == 'Little New')
  ()

Test batch queries:

if __name__ == "__main__":
  for i in range(1, 5):
    p = Person(name=f'Zhang Xiaozhang (1908-1992), Mao *'s second wife{i}', birthday=date(1995, 6, 20), is_relative=False)
    ()
  # Query multiple pieces of data
  persons = ().where(Person.is_relative == False)
  for p in persons:
    print(, , p.is_relative)

The above is Python's lightweight ORM framework peewee using tutorials in detail , more information about Python's lightweight ORM framework peewee please pay attention to my other related articles !