Skip to content

property_field

property_field(func)

Decorator to set a property like function on Model to be exposed as field in dict() and fastapi response. Although you can decorate a @property field like this and this will work, mypy validation will complain about this. Note that "fields" exposed like this do not go through validation.

Parameters:

Name Type Description Default
func Callable

decorated function to be exposed

required

Returns:

Type Description
Union[property, Callable]

decorated function passed in func param, with set property_field = True

Raises:

Type Description
ModelDefinitionError

if method has any other argument than self.

Source code in ormar\decorators\property_field.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def property_field(func: Callable) -> Union[property, Callable]:
    """
    Decorator to set a property like function on Model to be exposed
    as field in dict() and fastapi response.
    Although you can decorate a @property field like this and this will work,
    mypy validation will complain about this.
    Note that "fields" exposed like this do not go through validation.

    :raises ModelDefinitionError: if method has any other argument than self.
    :param func: decorated function to be exposed
    :type func: Callable
    :return: decorated function passed in func param, with set __property_field__ = True
    :rtype: 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 methods with no arguments"
            )
        func.__dict__["__property_field__"] = True
    return func