switch to decorator used to register property_fields and save it on Meta inner class to expose to cloned fastapi models

This commit is contained in:
collerek
2020-12-04 09:51:00 +01:00
parent 3b164c76de
commit 00ab8a6d1d
10 changed files with 175 additions and 143 deletions

View File

@ -0,0 +1,5 @@
from ormar.decorators.property_field import property_field
__all__ = [
"property_field",
]

View File

@ -0,0 +1,19 @@
import inspect
from collections.abc import Callable
from typing import Union
from ormar.exceptions import ModelDefinitionError
def property_field(func: Callable) -> Union[property, Callable]:
if isinstance(func, property): # pragma: no cover
func.fget.__property_field__ = True
else:
arguments = list(inspect.signature(func).parameters.keys())
if len(arguments) > 1 or arguments[0] != "self":
raise ModelDefinitionError(
"property_field decorator can be used "
"only on class methods with no arguments"
)
func.__dict__["__property_field__"] = True
return func