Skip to content

pydantic_mixin

PydanticMixin

Bases: RelationMixin

Source code in ormar\models\mixins\pydantic_mixin.py
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
class PydanticMixin(RelationMixin):

    __cache__: Dict[str, Type[pydantic.BaseModel]] = {}

    if TYPE_CHECKING:  # pragma: no cover
        __fields__: Dict[str, ModelField]
        _skip_ellipsis: Callable
        _get_not_excluded_fields: Callable

    @classmethod
    def get_pydantic(
        cls, *, include: Union[Set, Dict] = None, exclude: Union[Set, Dict] = None
    ) -> Type[pydantic.BaseModel]:
        """
        Returns a pydantic model out of ormar model.

        Converts also nested ormar models into pydantic models.

        Can be used to fully exclude certain fields in fastapi response and requests.

        :param include: fields of own and nested models to include
        :type include: Union[Set, Dict, None]
        :param exclude: fields of own and nested models to exclude
        :type exclude: Union[Set, Dict, None]
        """
        relation_map = translate_list_to_dict(cls._iterate_related_models())

        return cls._convert_ormar_to_pydantic(
            include=include, exclude=exclude, relation_map=relation_map
        )

    @classmethod
    def _convert_ormar_to_pydantic(
        cls,
        relation_map: Dict[str, Any],
        include: Union[Set, Dict] = None,
        exclude: Union[Set, Dict] = None,
    ) -> Type[pydantic.BaseModel]:
        if include and isinstance(include, Set):
            include = translate_list_to_dict(include)
        if exclude and isinstance(exclude, Set):
            exclude = translate_list_to_dict(exclude)
        fields_dict: Dict[str, Any] = dict()
        defaults: Dict[str, Any] = dict()
        fields_to_process = cls._get_not_excluded_fields(
            fields={*cls.Meta.model_fields.keys()}, include=include, exclude=exclude
        )
        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,
                defaults=defaults,
                include=include,
                exclude=exclude,
                relation_map=relation_map,
            )
            if field is not None:
                fields_dict[name] = field
        model = type(
            f"{cls.__name__}_{''.join(choices(string.ascii_uppercase, k=3))}",
            (pydantic.BaseModel,),
            {"__annotations__": fields_dict, **defaults},
        )
        model = cast(Type[pydantic.BaseModel], model)
        cls._copy_field_validators(model=model)
        cls.__cache__[cache_key] = model
        return model

    @classmethod
    def _determine_pydantic_field_type(
        cls,
        name: str,
        defaults: Dict,
        include: Union[Set, Dict, None],
        exclude: Union[Set, Dict, None],
        relation_map: Dict[str, Any],
    ) -> Any:
        field = cls.Meta.model_fields[name]
        target: Any = None
        if field.is_relation and name in relation_map:  # type: ignore
            target = field.to._convert_ormar_to_pydantic(
                include=cls._skip_ellipsis(include, name),
                exclude=cls._skip_ellipsis(exclude, name),
                relation_map=cls._skip_ellipsis(
                    relation_map, name, default_return=dict()
                ),
            )
            if field.is_multi or field.virtual:
                target = List[target]  # type: ignore
        elif not field.is_relation:
            defaults[name] = cls.__fields__[name].field_info
            target = field.__type__
        if target is not None and field.nullable:
            target = Optional[target]
        return target

    @classmethod
    def _copy_field_validators(cls, model: Type[pydantic.BaseModel]) -> None:
        """
        Copy field validators from ormar model to generated pydantic model.
        """
        for field_name, field in model.__fields__.items():
            if (
                field_name not in cls.__fields__
                or cls.Meta.model_fields[field_name].is_relation
            ):
                continue
            validators = cls.__fields__[field_name].validators
            already_attached = [
                validator.__wrapped__ for validator in field.validators  # type: ignore
            ]
            validators_to_copy = [
                validator
                for validator in validators
                if validator.__wrapped__ not in already_attached  # type: ignore
            ]
            field.validators.extend(copy.deepcopy(validators_to_copy))
            class_validators = cls.__fields__[field_name].class_validators
            field.class_validators.update(copy.deepcopy(class_validators))
            field.pre_validators = copy.deepcopy(
                cls.__fields__[field_name].pre_validators
            )
            field.post_validators = copy.deepcopy(
                cls.__fields__[field_name].post_validators
            )

get_pydantic(*, include=None, exclude=None) classmethod

Returns a pydantic model out of ormar model.

Converts also nested ormar models into pydantic models.

Can be used to fully exclude certain fields in fastapi response and requests.

Parameters:

Name Type Description Default
include Union[Set, Dict]

fields of own and nested models to include

None
exclude Union[Set, Dict]

fields of own and nested models to exclude

None
Source code in ormar\models\mixins\pydantic_mixin.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@classmethod
def get_pydantic(
    cls, *, include: Union[Set, Dict] = None, exclude: Union[Set, Dict] = None
) -> Type[pydantic.BaseModel]:
    """
    Returns a pydantic model out of ormar model.

    Converts also nested ormar models into pydantic models.

    Can be used to fully exclude certain fields in fastapi response and requests.

    :param include: fields of own and nested models to include
    :type include: Union[Set, Dict, None]
    :param exclude: fields of own and nested models to exclude
    :type exclude: Union[Set, Dict, None]
    """
    relation_map = translate_list_to_dict(cls._iterate_related_models())

    return cls._convert_ormar_to_pydantic(
        include=include, exclude=exclude, relation_map=relation_map
    )