Skip to content

alias_mixin

AliasMixin

Used to translate field names into database column names.

Source code in ormar/models/mixins/alias_mixin.py
 4
 5
 6
 7
 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
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
class AliasMixin:
    """
    Used to translate field names into database column names.
    """

    if TYPE_CHECKING:  # pragma: no cover
        from ormar import ModelMeta

        Meta: ModelMeta

    @classmethod
    def get_column_alias(cls, field_name: str) -> str:
        """
        Returns db alias (column name in db) for given ormar field.
        For fields without alias field name is returned.
        :param field_name: name of the field to get alias from
        :type field_name: str
        :return: alias (db name) if set, otherwise passed name
        :rtype: str
        """
        field = cls.Meta.model_fields.get(field_name)
        return field.get_alias() if field is not None else field_name

    @classmethod
    def get_column_name_from_alias(cls, alias: str) -> str:
        """
        Returns ormar field name for given db alias (column name in db).
        If field do not have alias it's returned as is.
        :param alias:
        :type alias: str
        :return: field name if set, otherwise passed alias (db name)
        :rtype: str
        """
        for field_name, field in cls.Meta.model_fields.items():
            if field.get_alias() == alias:
                return field_name
        return alias  # if not found it's not an alias but actual name

    @classmethod
    def translate_columns_to_aliases(cls, new_kwargs: Dict) -> Dict:
        """
        Translates dictionary of model fields changing field names into aliases.
        If field has no alias the field name remains intact.
        Only fields present in the dictionary are translated.
        :param new_kwargs: dict with fields names and their values
        :type new_kwargs: Dict
        :return: dict with aliases and their values
        :rtype: Dict
        """
        for field_name, field in cls.Meta.model_fields.items():
            if field_name in new_kwargs:
                new_kwargs[field.get_alias()] = new_kwargs.pop(field_name)
        return new_kwargs

    @classmethod
    def translate_aliases_to_columns(cls, new_kwargs: Dict) -> Dict:
        """
        Translates dictionary of model fields changing aliases into field names.
        If field has no alias the alias is already a field name.
        Only fields present in the dictionary are translated.
        :param new_kwargs: dict with aliases and their values
        :type new_kwargs: Dict
        :return: dict with fields names and their values
        :rtype: Dict
        """
        for field_name, field in cls.Meta.model_fields.items():
            if field.get_alias() and field.get_alias() in new_kwargs:
                new_kwargs[field_name] = new_kwargs.pop(field.get_alias())
        return new_kwargs

get_column_alias(field_name) classmethod

Returns db alias (column name in db) for given ormar field. For fields without alias field name is returned.

Parameters:

Name Type Description Default
field_name str

name of the field to get alias from

required

Returns:

Type Description
str

alias (db name) if set, otherwise passed name

Source code in ormar/models/mixins/alias_mixin.py
14
15
16
17
18
19
20
21
22
23
24
25
@classmethod
def get_column_alias(cls, field_name: str) -> str:
    """
    Returns db alias (column name in db) for given ormar field.
    For fields without alias field name is returned.
    :param field_name: name of the field to get alias from
    :type field_name: str
    :return: alias (db name) if set, otherwise passed name
    :rtype: str
    """
    field = cls.Meta.model_fields.get(field_name)
    return field.get_alias() if field is not None else field_name

get_column_name_from_alias(alias) classmethod

Returns ormar field name for given db alias (column name in db). If field do not have alias it's returned as is.

Parameters:

Name Type Description Default
alias str required

Returns:

Type Description
str

field name if set, otherwise passed alias (db name)

Source code in ormar/models/mixins/alias_mixin.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@classmethod
def get_column_name_from_alias(cls, alias: str) -> str:
    """
    Returns ormar field name for given db alias (column name in db).
    If field do not have alias it's returned as is.
    :param alias:
    :type alias: str
    :return: field name if set, otherwise passed alias (db name)
    :rtype: str
    """
    for field_name, field in cls.Meta.model_fields.items():
        if field.get_alias() == alias:
            return field_name
    return alias  # if not found it's not an alias but actual name

translate_aliases_to_columns(new_kwargs) classmethod

Translates dictionary of model fields changing aliases into field names. If field has no alias the alias is already a field name. Only fields present in the dictionary are translated.

Parameters:

Name Type Description Default
new_kwargs Dict

dict with aliases and their values

required

Returns:

Type Description
Dict

dict with fields names and their values

Source code in ormar/models/mixins/alias_mixin.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@classmethod
def translate_aliases_to_columns(cls, new_kwargs: Dict) -> Dict:
    """
    Translates dictionary of model fields changing aliases into field names.
    If field has no alias the alias is already a field name.
    Only fields present in the dictionary are translated.
    :param new_kwargs: dict with aliases and their values
    :type new_kwargs: Dict
    :return: dict with fields names and their values
    :rtype: Dict
    """
    for field_name, field in cls.Meta.model_fields.items():
        if field.get_alias() and field.get_alias() in new_kwargs:
            new_kwargs[field_name] = new_kwargs.pop(field.get_alias())
    return new_kwargs

translate_columns_to_aliases(new_kwargs) classmethod

Translates dictionary of model fields changing field names into aliases. If field has no alias the field name remains intact. Only fields present in the dictionary are translated.

Parameters:

Name Type Description Default
new_kwargs Dict

dict with fields names and their values

required

Returns:

Type Description
Dict

dict with aliases and their values

Source code in ormar/models/mixins/alias_mixin.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@classmethod
def translate_columns_to_aliases(cls, new_kwargs: Dict) -> Dict:
    """
    Translates dictionary of model fields changing field names into aliases.
    If field has no alias the field name remains intact.
    Only fields present in the dictionary are translated.
    :param new_kwargs: dict with fields names and their values
    :type new_kwargs: Dict
    :return: dict with aliases and their values
    :rtype: Dict
    """
    for field_name, field in cls.Meta.model_fields.items():
        if field_name in new_kwargs:
            new_kwargs[field.get_alias()] = new_kwargs.pop(field_name)
    return new_kwargs