Skip to content

merge_mixin

MergeModelMixin

Used to collect models instances returned by database, but already initialized to ormar Models.

Rows of one model returned by a join are materialized into a single shared instance (see RowRegistry), so the same main model can repeat in the list of materialized rows; in the end all main models should be unique.

Source code in ormar/models/mixins/merge_mixin.py
Python
 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
class MergeModelMixin:
    """
    Used to collect models instances returned by database,
    but already initialized to ormar Models.

    Rows of one model returned by a join are materialized into a single shared
    instance (see ``RowRegistry``), so the same main model can repeat in the
    list of materialized rows; in the end all main models should be unique.
    """

    @classmethod
    def merge_instances_list(
        cls,
        result_rows: list["Model"],
        *,
        excludable: "ExcludableItems",
    ) -> list["Model"]:
        """
        Collapses a list of materialized rows into a list of unique models,
        keeping the order of first occurrence.

        Each top-level model also has the queryset's ``ExcludableItems``
        attached via the ``__ormar_excludable__`` slot so a bare ``model_dump()``
        on the result can resolve the (lazily cached) flatten map without going
        back through the queryset.

        :param result_rows: list of models materialized from consecutive rows,
            where rows of the same main model resolve to the same instance
        :type result_rows: list["Model"]
        :param excludable: ExcludableItems to attach to every top-level
            instance via the ``__ormar_excludable__`` slot
        :type excludable: ExcludableItems
        :return: list of unique main models
        :rtype: list["Model"]
        """
        merged_rows: list["Model"] = []
        seen: set[int] = set()
        for model in result_rows:
            if id(model) in seen:
                continue
            seen.add(id(model))
            object.__setattr__(model, "__ormar_excludable__", excludable)
            merged_rows.append(model)
        return merged_rows

merge_instances_list(result_rows, *, excludable) classmethod

Collapses a list of materialized rows into a list of unique models, keeping the order of first occurrence.

Each top-level model also has the queryset's ExcludableItems attached via the __ormar_excludable__ slot so a bare model_dump() on the result can resolve the (lazily cached) flatten map without going back through the queryset.

Parameters:

Name Type Description Default
result_rows list[Model]

list of models materialized from consecutive rows, where rows of the same main model resolve to the same instance

required
excludable ExcludableItems

ExcludableItems to attach to every top-level instance via the __ormar_excludable__ slot

required

Returns:

Type Description
list["Model"]

list of unique main models

Source code in ormar/models/mixins/merge_mixin.py
Python
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
@classmethod
def merge_instances_list(
    cls,
    result_rows: list["Model"],
    *,
    excludable: "ExcludableItems",
) -> list["Model"]:
    """
    Collapses a list of materialized rows into a list of unique models,
    keeping the order of first occurrence.

    Each top-level model also has the queryset's ``ExcludableItems``
    attached via the ``__ormar_excludable__`` slot so a bare ``model_dump()``
    on the result can resolve the (lazily cached) flatten map without going
    back through the queryset.

    :param result_rows: list of models materialized from consecutive rows,
        where rows of the same main model resolve to the same instance
    :type result_rows: list["Model"]
    :param excludable: ExcludableItems to attach to every top-level
        instance via the ``__ormar_excludable__`` slot
    :type excludable: ExcludableItems
    :return: list of unique main models
    :rtype: list["Model"]
    """
    merged_rows: list["Model"] = []
    seen: set[int] = set()
    for model in result_rows:
        if id(model) in seen:
            continue
        seen.add(id(model))
        object.__setattr__(model, "__ormar_excludable__", excludable)
        merged_rows.append(model)
    return merged_rows