Cherry ORM
Python 异步 ORM
简体中文 · English
简介
Cherry ORM 是一个 Python 的异步对象关系映射(ORM)库,它基于 SQLAlchemy Core 和 Pydantic V2 构建。
它的一切设计都是为了简单易用,极大地减少开发者的数据库操作成本,提高开发效率,让开发者更专注于业务逻辑的实现。
安装
- 使用 pip:
pip install cherry-orm - 使用 Poetry:
poetry add cherry-orm - 使用 PDM:
pdm add cherry-orm
文档
-> 文档地址
示例
from datetime import date
from typing import List, Optional
import cherry
db = cherry.Database("sqlite+aiosqlite:///:memory:")
class Student(cherry.Model):
id: int = cherry.Field(primary_key=True)
name: str = cherry.Field(unique=True, index=True)
age: int
birthday: date = cherry.Field(default_factory=date.today)
school: cherry.ForeignKey[Optional["School"]] = None
cherry_config = cherry.CherryConfig(tablename="student", database=db)
class School(cherry.Model):
id: cherry.PrimaryKey[int]
name: str = cherry.Field(unique=True, index=True)
students: cherry.ReverseRelation[List[Student]] = []
cherry_config = cherry.CherryConfig(tablename="school", database=db)
async def main():
await db.init()
# 插入
school = await School(id=1, name="school 1").insert()
student1 = await Student(id=1, name="student 1", age=15, school=school).insert()
await Student(id=2, name="student 2", age=18, school=school).insert()
await Student(id=3, name="student 3", age=20, school=school).insert()
# 更新
student1.age += 1
await student1.save()
# or
await student1.update(age=19)
# 获取关联的模型
await school.fetch_related(School.students)
assert len(school.students) == 3
# 条件查询
# Pythonic 风格
student2: Student = await Student.filter(Student.name == "student 2").get()
# Django 风格
student2: Student = await Student.filter(name="student 2").get()
students: List[Student] = await Student.filter(Student.age >= 18).all()
# 聚合查询
student_nums: int = await Student.filter(Student.age >= 18).count()
assert len(students) == student_nums
student_age_avg: Optional[int] = await Student.select().avg(Student.age)
# 查询时预取关联模型
student_with_school: Student = (
await Student.filter(Student.name == "student 3")
.prefetch_related(Student.school)
.get()
)
# 选择更新
await Student.select().update(birthday=date(2023, 10, 1))
# 选择删除
await Student.filter(Student.age >= 20).delete()
Metadata
Release files for cherry-orm 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cherry_orm-1.0.0.tar.gz | 25.0 kB | Without provenance | Details for cherry_orm-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cherry_orm-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for cherry_orm-1.0.0-py3-none-any.whl |
Total release size: 53.8 kB
Release files / cherry_orm-1.0.0.tar.gz
| Download URL | cherry_orm-1.0.0.tar.gz |
|---|---|
| Size | 25.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e98034100a0d04057e1a524a30bcac1e15e9eb60dea4700626e69237710cf846
|
|
BLAKE2b-256 checksum How to use checksums |
f5b9e16a159caa7d0bca2c967efd497bc9ef72f3a14651754018ac3ca176fc89
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|
Release files / cherry_orm-1.0.0-py3-none-any.whl
| Download URL | cherry_orm-1.0.0-py3-none-any.whl |
|---|---|
| Size | 28.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2549e96e1e0d45555c9a95e212c1924af4418eb4925e02e8621abe529cd5a0f0
|
|
BLAKE2b-256 checksum How to use checksums |
3a583ff6b2c80b2a0b165351e49316fc0a0f00c9de6b3e5a41607b2a2adf3915
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|
Security for cherry-orm 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.