peewee -- a django style ORM


django model

https://docs.djangoproject.com/en/3.2/topics/db/models/

django的ORM工具, 具有非常简洁的接口,遵从django快速开发的理念。

但是此工具是跟django深度绑定的, 很难独立使用。

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.

The basics:

  • Each model is a Python class that subclasses django.db.models.Model.
  • Each attribute of the model represents a database field.
  • With all of this, Django gives you an automatically-generated database-access API; see Making queries.
from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

https://www.pythoncentral.io/sqlalchemy-vs-orms/#:~:text=%20Comparison%20Between%20Python%20ORMs%20%201%20SQLObject.,the%20unit-of-work%20concept%20which%20is%20prevalent...%20More%20

Django's ORM

Pros:

  1. Easy-to-use with a short learning curve
  2. Tightly integrated with Django to make it the de-factor standard when dealing with databases in Django

Cons:

  1. Does not handle complex queries very well; forcing the developer to go back to raw SQL
  2. Tightly integrated with Django; making it hard to use outside of a Django context

SQLAlchemy

https://www.sqlalchemy.org/

企业级持久化套件, 设计上高效和高性能的数据库访问。

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.

It provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.

https://www.pythoncentral.io/sqlalchemy-vs-orms/#:~:text=%20Comparison%20Between%20Python%20ORMs%20%201%20SQLObject.,the%20unit-of-work%20concept%20which%20is%20prevalent...%20More%20

API比较重量,学习曲线长。

Pros:

  1. Enterprise-level APIs; making the code robust and adaptable
  2. Flexible design; making it painless to write complex queries

Cons:

  1. The Unit-of-work concept is not common
  2. A heavyweight API; leading to a long learning curve

migration

https://alembic.sqlalchemy.org/en/latest/autogenerate.html

peewee

https://stackoverflow.com/questions/53428/what-are-some-good-python-orm-solutions

正解: 拥有django风格。

If you're looking for lightweight and are already familiar with django-style declarative models, check out peewee: https://github.com/coleifer/peewee

import datetime
from peewee import *

class Blog(Model):
    name = CharField()

class Entry(Model):
    blog = ForeignKeyField(Blog)
    title = CharField()
    body = TextField()
    pub_date = DateTimeField(default=datetime.datetime.now)

# query it like django
Entry.filter(blog__name='Some great blog')

# or programmatically for finer-grained control
Entry.select().join(Blog).where(Blog.name == 'Some awesome blog')

https://www.pythoncentral.io/sqlalchemy-vs-orms/#:~:text=%20Comparison%20Between%20Python%20ORMs%20%201%20SQLObject.,the%20unit-of-work%20concept%20which%20is%20prevalent...%20More%20

轻量,易于集成。

Pros:

  1. A Django-ish API; making it easy-to-use
  2. A lightweight implementation; making it easy to integrate with any web framework

Cons:

  1. Does not support automatic schema migrations
  2. Many-to-Many queries are not intuitive to write

Reference

https://github.com/coleifer/peewee

http://docs.peewee-orm.com/en/latest/peewee/quickstart.html

DB migration

https://github.com/keredson/peewee-db-evolve

Peewee DB Evolve

Diffs your models against your database, and outputs SQL to (non-destructively) update your schema.

Think of it as db.create_tables() on steriods (which doesn't drop your database).

You can also think of it as schema migrations, without having to actually write the migrations.

使用样例

https://github.com/keredson/peewee-db-evolve/tree/master/examples/hello_world