* add __cache__ property into PydanticMixin * add new test for exclude fields on relations * fix the return type from __cache__ property * run pre-commit lints/checks Co-authored-by: collerek <collerek@gmail.com>
This commit is contained in:
@ -22,6 +22,9 @@ from ormar.queryset.utils import translate_list_to_dict
|
||||
|
||||
|
||||
class PydanticMixin(RelationMixin):
|
||||
|
||||
__cache__: Dict[str, Type[pydantic.BaseModel]] = {}
|
||||
|
||||
if TYPE_CHECKING: # pragma: no cover
|
||||
__fields__: Dict[str, ModelField]
|
||||
_skip_ellipsis: Callable
|
||||
@ -68,6 +71,11 @@ class PydanticMixin(RelationMixin):
|
||||
fields_to_process.sort(
|
||||
key=lambda x: list(cls.Meta.model_fields.keys()).index(x)
|
||||
)
|
||||
|
||||
cache_key = f"{cls.__name__}_{str(include)}_{str(exclude)}"
|
||||
if cache_key in cls.__cache__:
|
||||
return cls.__cache__[cache_key]
|
||||
|
||||
for name in fields_to_process:
|
||||
field = cls._determine_pydantic_field_type(
|
||||
name=name,
|
||||
@ -85,6 +93,7 @@ class PydanticMixin(RelationMixin):
|
||||
)
|
||||
model = cast(Type[pydantic.BaseModel], model)
|
||||
cls._copy_field_validators(model=model)
|
||||
cls.__cache__[cache_key] = model
|
||||
return model
|
||||
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user