xn-model 1.0.7__tar.gz → 1.0.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {xn_model-1.0.7/xn_model.egg-info → xn_model-1.0.8}/PKG-INFO +1 -1
- {xn_model-1.0.7 → xn_model-1.0.8}/x_model/models.py +13 -12
- {xn_model-1.0.7 → xn_model-1.0.8/xn_model.egg-info}/PKG-INFO +1 -1
- {xn_model-1.0.7 → xn_model-1.0.8}/.env.sample +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/.gitignore +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/.pre-commit-config.yaml +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/README.md +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/makefile +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/pyproject.toml +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/setup.cfg +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/tests/__init__.py +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/tests/test_db.py +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/x_model/__init__.py +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/x_model/field.py +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/x_model/func.py +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/x_model/types.py +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/xn_model.egg-info/SOURCES.txt +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/xn_model.egg-info/dependency_links.txt +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/xn_model.egg-info/requires.txt +0 -0
- {xn_model-1.0.7 → xn_model-1.0.8}/xn_model.egg-info/top_level.txt +0 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
from datetime import datetime
|
|
2
|
+
from typing import Self
|
|
2
3
|
|
|
3
4
|
from pydantic import ConfigDict, BaseModel
|
|
4
5
|
from tortoise import Model as TortModel
|
|
@@ -16,8 +17,8 @@ class TsTrait:
|
|
|
16
17
|
class Model(TortModel):
|
|
17
18
|
id: int = IntField(True)
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
_out_type: type[BaseModel] = None # overridable
|
|
21
|
+
_in_type: type[BaseModel] = None # overridable
|
|
21
22
|
_name: tuple[str] = ("name",)
|
|
22
23
|
_sorts: tuple[str] = ("-id",)
|
|
23
24
|
|
|
@@ -25,31 +26,31 @@ class Model(TortModel):
|
|
|
25
26
|
return sep.join(getattr(self, name_fragment) for name_fragment in self._name)
|
|
26
27
|
|
|
27
28
|
@classmethod
|
|
28
|
-
def
|
|
29
|
-
if not cls.
|
|
29
|
+
def out_type(cls):
|
|
30
|
+
if not cls._out_type:
|
|
30
31
|
cls._out = pydantic_model_creator(cls, name=cls.__name__ + "Out")
|
|
31
|
-
return cls.
|
|
32
|
+
return cls._out_type
|
|
32
33
|
|
|
33
34
|
@classmethod
|
|
34
|
-
def
|
|
35
|
-
if not cls.
|
|
36
|
-
cls.
|
|
35
|
+
def in_type(cls):
|
|
36
|
+
if not cls._in_type:
|
|
37
|
+
cls._inn = pydantic_model_creator(
|
|
37
38
|
cls, name=cls.__name__ + "In", exclude_readonly=True, meta_override=cls.PydanticMetaIn
|
|
38
39
|
)
|
|
39
|
-
return cls.
|
|
40
|
+
return cls._in_type
|
|
40
41
|
|
|
41
42
|
# # # CRUD Methods # # #
|
|
42
43
|
@classmethod
|
|
43
44
|
async def get_one(cls, id_: int) -> PydanticModel:
|
|
44
45
|
if obj := await cls.get_or_none(id=id_):
|
|
45
|
-
return await cls.
|
|
46
|
+
return await cls.out_type().from_tortoise_orm(obj)
|
|
46
47
|
raise LookupError(f"{cls.__name__}#{id_} not found")
|
|
47
48
|
|
|
48
49
|
async def one(self) -> PydanticModel:
|
|
49
|
-
return await self.
|
|
50
|
+
return await self.out_type().from_tortoise_orm(self)
|
|
50
51
|
|
|
51
52
|
@classmethod
|
|
52
|
-
async def get_or_create_by_name(cls, name: str, attr_name: str = None, def_dict: dict = None) ->
|
|
53
|
+
async def get_or_create_by_name(cls, name: str, attr_name: str = None, def_dict: dict = None) -> Self:
|
|
53
54
|
attr_name = attr_name or list(cls._name)[0]
|
|
54
55
|
if not (obj := await cls.get_or_none(**{attr_name: name})):
|
|
55
56
|
next_id = (await cls.all().order_by("-id").first()).id + 1
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|