Skip to content

Commit

Permalink
Merge branch 'pydantic-v2'
Browse files Browse the repository at this point in the history
  • Loading branch information
CMHopeSunshine committed May 16, 2024
2 parents 4a11db0 + 6351a33 commit 95d264c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 39 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,15 @@ class Student(cherry.Model):
birthday: date = cherry.Field(default_factory=date.today)
school: cherry.ForeignKey[Optional["School"]] = None

class Meta:
database = db
tablename = "student"
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]] = []

class Meta:
database = db
tablename = "school"
cherry_config = cherry.CherryConfig(tablename="school", database=db)


async def main():
Expand Down
8 changes: 2 additions & 6 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,15 @@ class Student(cherry.Model):
birthday: date = cherry.Field(default_factory=date.today)
school: cherry.ForeignKey[Optional["School"]] = None

class Meta:
database = db
tablename = "student"
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]] = []

class Meta:
database = db
tablename = "school"
cherry_config = cherry.CherryConfig(tablename="school", database=db)


async def main():
Expand Down
18 changes: 9 additions & 9 deletions cherry/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ def __new__(
cls.__meta__.database = database
if not abstract:
database.add_model(cls)
cls.__meta__.primary_key = tuple(
field_name
for field_name, field in cls.model_fields.items()
if isinstance(field, BaseField) and field.primary_key
)
cls.__meta__.primary_key = tuple(
field_name
for field_name, field in cls.model_fields.items()
if isinstance(field, BaseField) and field.primary_key
)

if len(cls.__meta__.primary_key) == 0:
raise PrimaryKeyMissingError(
f"Model {cls} must have at least one primary key",
)
if len(cls.__meta__.primary_key) == 0:
raise PrimaryKeyMissingError(
f"Model {cls} must have at least one primary key",
)

return cls

Expand Down
14 changes: 5 additions & 9 deletions cherry/typing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
from collections.abc import Mapping
from typing import (
Any,
Dict,
List,
Literal,
Mapping,
Tuple,
Type,
TYPE_CHECKING,
TypeVar,
Union,
Expand Down Expand Up @@ -36,9 +32,9 @@
Ts = TypeVarTuple("Ts")
T_MODEL = TypeVar("T_MODEL", bound="Model")

ModelType = Type["Model"]
DictStrAny: TypeAlias = Dict[str, Any]
TupleAny: TypeAlias = Tuple[Any, ...]
ModelType = type["Model"]
DictStrAny: TypeAlias = dict[str, Any]
TupleAny: TypeAlias = tuple[Any, ...]
AnyMapping: TypeAlias = Mapping[Any, Any]
ClauseListType: TypeAlias = List[Union[BinaryExpression[bool], "ModelClause"]]
ClauseListType: TypeAlias = list[Union[BinaryExpression[bool], "ModelClause"]]
OptionalClause: TypeAlias = Union[BooleanClauseList, BinaryExpression, None]
14 changes: 5 additions & 9 deletions example/example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import date
from typing import List, Optional
from typing import Optional

import cherry

Expand All @@ -13,19 +13,15 @@ class Student(cherry.Model):
birthday: date = cherry.Field(default_factory=date.today)
school: cherry.ForeignKey[Optional["School"]] = None

class Meta:
database = db
tablename = "student"
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]] = []
students: cherry.ReverseRelation[list[Student]] = []

class Meta:
database = db
tablename = "school"
cherry_config = cherry.CherryConfig(tablename="school", database=db)


async def main():
Expand Down Expand Up @@ -53,7 +49,7 @@ async def main():
# Django 风格
student2: Student = await Student.filter(name="student 2").get()

students: List[Student] = await Student.filter(Student.age >= 18).all()
students: list[Student] = await Student.filter(Student.age >= 18).all()

# 聚合查询
student_nums: int = await Student.filter(Student.age >= 18).count()
Expand Down

0 comments on commit 95d264c

Please sign in to comment.