SQLAlchemy is too big to try Peewee

Keywords: Database Django Python SQL

Shenzhen Python Training: SQL Alchemy is too big to try Peewee
SQLAlchemy has powerful functions and rich documents. It is a heavyweight ORM framework. This article introduces a small, clean, lightweight ORM framework Peewee, which supports Python 2.7 + and 3.4 +, and supports SQLite, MySQL and PostgreSQL. If you are familiar with Django's ORM, Peewee's learning cost will be very low.

install

pip install peewee

Model Definition

from peewee import *

db = SqliteDatabase('people.db')

class BaseModel(Model):
    class Meta:
        database = db

class Person(BaseModel):
    name = CharField(verbose_name='Full name', max_length=10, null=False, index=True)
    gender = IntegerField(verbose_name='Sex', null=False, default=1)
    birthday = DateField(verbose_name='Birthday', null=True, default=None)

    class Meta:
        table_name = 'people'

Firstly, we define our model class Person (students who have used Django must be familiar with the definition of this model, which is very similar to the definition of model in Django). We use SqliteDatabase to specify the database people.db used.

Then the data Field of Person table is defined. If the primary key is not specified, peewee will automatically create an id Field for us as the primary key. Each Field has several parameters to configure, such as length, null and default, index and unique.

Create database tables

Person.create_table()
# or
db.create_tables([Person])

Operating database

  • increase

    Create the instance directly, and then call the instance method save().

    You can also create instances and save them by creating () class methods.

    p = Person(name='tom', gender=1, birthday=date(2000, 1, 1))
    p.save()
    
    jerry = Person.create(name='jerry', gender=0, birthday=date(1999, 12, 1))
    
  • Delete

    Conditional deletion is performed using delete().where().execute(), where() is the deletion condition, and execute() performs the deletion operation.

    If the instance object is already queried, the instance method delete_instance() is called to delete it.

    # Delete data named tom
    Person.delete().where(Person.name=='tom').execute()
    
    # Objects that have been instantiated call delete_instance() for deletion
    p = Person(name='tom', gender=1, birthday=date(2000, 1, 1))
    p.save()
    p.delete_instance()
    
  • change

    Conditional updates are performed using update().wahere().excute(). For the data objects that have been queried, after modifying the object attributes, save() updates directly.

    # If the object has been instantiated and has the primary key of id, save is the update operation after modifying the attribute.
    p = Person(name='tom', gender=1, birthday=date(2000, 1, 1))
    p.save()
    p.gender = 0
    p.save()
    
    # Update jerry's birthday data
    q = Person.update({Person.birthday: date(1999, 12, 12)}).where(Person.name=='jerry')
    q.execute()
    
  • check

    A single data query uses Person.get() or Person.select().where().get().

    Multiple data queries use Person.select().where()

    # Query single data
    p = Person.select().where(Person.name=='tom').get()
    print(p.name, p.gender, p.birthday)
    
    # Use the abbreviation Model.get()
    p = Person.get(Person.name=='tom')
    print(p.name, p.gender, p.birthday)
    
    # Query multiple data
    people = Person.select().where(Person.gender==1)
    for p in people:
        print(p.name, p.gender, p.birthday)
    

Posted by filburt1 on Mon, 26 Aug 2019 00:12:54 -0700