Skip to content

Commit

Permalink
feat: 支持获取属性字段名称
Browse files Browse the repository at this point in the history
  • Loading branch information
FHU-yezi committed Mar 21, 2024
1 parent d28092e commit 6a828e0
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions utils/document_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from bson import ObjectId
from motor.core import AgnosticCollection
from msgspec import Struct, convert, to_builtins
from msgspec.inspect import type_info
from pymongo import IndexModel
from typing_extensions import Self

Expand All @@ -39,6 +40,17 @@ class Meta(Struct):
collection: ClassVar[AgnosticCollection]
indexes: ClassVar[List[IndexModel]]

@classmethod
def _get_field_name(cls, field: object) -> str:
attr_name = field.__qualname__.split(".")[-1]
for field_obj in type_info(cls).fields: # type: ignore
if field_obj.name != attr_name:
continue

return field_obj.encode_name

raise ValueError("未找到属性对应的字段名称")

@classmethod
def get_collection(cls) -> AgnosticCollection:
return cls.Meta.collection
Expand Down

0 comments on commit 6a828e0

Please sign in to comment.