Skip to content

models.mixins.merge_mixin

MergeModelMixin Objects

1
class MergeModelMixin()

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

Models can duplicate during joins when parent model has multiple child rows, in the end all parent (main) models should be unique.

merge_instances_list

1
2
 | @classmethod
 | merge_instances_list(cls, result_rows: List["Model"]) -> List["Model"]

Merges a list of models into list of unique models.

Models can duplicate during joins when parent model has multiple child rows, in the end all parent (main) models should be unique.

Arguments:

  • result_rows: list of already initialized Models with child models populated, each instance is one row in db and some models can duplicate :type result_rows: List["Model"]

Returns:

list of merged models where each main model is unique :rtype: List["Model"]

merge_two_instances

1
2
 | @classmethod
 | merge_two_instances(cls, one: "Model", other: "Model", relation_map: Dict = None) -> "Model"

Merges current (other) Model and previous one (one) and returns the current Model instance with data merged from previous one.

If needed it's calling itself recurrently and merges also children models.

Arguments:

  • relation_map: map of models relations to follow :type relation_map: Dict
  • one: previous model instance :type one: Model
  • other: current model instance :type other: Model

Returns:

current Model instance with data merged from previous one. :rtype: Model

_merge_items_lists

1
2
 | @classmethod
 | _merge_items_lists(cls, field_name: str, current_field: List, other_value: List, relation_map: Optional[Dict]) -> List

Takes two list of nested models and process them going deeper according with the map.

If model from one's list is in other -> they are merged with relations to follow passed from map.

If one's model is not in other it's simply appended to the list.

Arguments:

  • field_name: name of the current relation field :type field_name: str
  • current_field: list of nested models from one model :type current_field: List[Model]
  • other_value: list of nested models from other model :type other_value: List[Model]
  • relation_map: map of relations to follow :type relation_map: Dict

Returns:

merged list of models :rtype: List[Model]