Skip to content

models.mixins.prefetch_mixin

PrefetchQueryMixin Objects

1
class PrefetchQueryMixin(RelationMixin)

Used in PrefetchQuery to extract ids and names of models to prefetch.

get_clause_target_and_filter_column_name

1
2
 | @staticmethod
 | get_clause_target_and_filter_column_name(parent_model: Type["Model"], target_model: Type["Model"], reverse: bool, related: str) -> Tuple[Type["Model"], str]

Returns Model on which query clause should be performed and name of the column.

Arguments:

  • parent_model: related model that the relation lead to :type parent_model: Type[Model]
  • target_model: model on which query should be perfomed :type target_model: Type[Model]
  • reverse: flag if the relation is reverse :type reverse: bool
  • related: name of the relation field :type related: str

Returns:

Model on which query clause should be performed and name of the column :rtype: Tuple[Type[Model], str]

get_column_name_for_id_extraction

1
2
 | @staticmethod
 | get_column_name_for_id_extraction(parent_model: Type["Model"], reverse: bool, related: str, use_raw: bool) -> str

Returns name of the column that should be used to extract ids from model. Depending on the relation side it's either primary key column of parent model or field name specified by related parameter.

Arguments:

  • parent_model: model from which id column should be extracted :type parent_model: Type[Model]
  • reverse: flag if the relation is reverse :type reverse: bool
  • related: name of the relation field :type related: str
  • use_raw: flag if aliases or field names should be used :type use_raw: bool

Returns:

:rtype:

1
2
 | @classmethod
 | get_related_field_name(cls, target_field: "ForeignKeyField") -> str

Returns name of the relation field that should be used in prefetch query. This field is later used to register relation in prefetch query, populate relations dict, and populate nested model in prefetch query.

Arguments:

  • target_field: relation field that should be used in prefetch :type target_field: Type[BaseField]

Returns:

name of the field :rtype: str

get_filtered_names_to_extract

1
2
 | @classmethod
 | get_filtered_names_to_extract(cls, prefetch_dict: Dict) -> List

Returns list of related fields names that should be followed to prefetch related models from.

List of models is translated into dict to assure each model is extracted only once in one query, that's why this function accepts prefetch_dict not list.

Only relations from current model are returned.

Arguments:

  • prefetch_dict: dictionary of fields to extract :type prefetch_dict: Dict

Returns:

list of fields names to extract :rtype: List