Skip to content

models.model_row

ModelRow Objects

1
class ModelRow(NewBaseModel)

from_row

1
2
 | @classmethod
 | from_row(cls, row: ResultProxy, source_model: Type["Model"], select_related: List = None, related_models: Any = None, related_field: "ForeignKeyField" = None, excludable: ExcludableItems = None, current_relation_str: str = "", proxy_source_model: Optional[Type["Model"]] = None, used_prefixes: List[str] = None) -> Optional["Model"]

Model method to convert raw sql row from database into ormar.Model instance. Traverses nested models if they were specified in select_related for query.

Called recurrently and returns model instance if it's present in the row. Note that it's processing one row at a time, so if there are duplicates of parent row that needs to be joined/combined (like parent row in sql join with 2+ child rows) instances populated in this method are later combined in the QuerySet. Other method working directly on raw database results is in prefetch_query, where rows are populated in a different way as they do not have nested models in result.

Arguments:

  • used_prefixes: list of already extracted prefixes :type used_prefixes: List[str]
  • proxy_source_model: source model from which querysetproxy is constructed :type proxy_source_model: Optional[Type["ModelRow"]]
  • excludable: structure of fields to include and exclude :type excludable: ExcludableItems
  • current_relation_str: name of the relation field :type current_relation_str: str
  • source_model: model on which relation was defined :type source_model: Type[Model]
  • row: raw result row from the database :type row: ResultProxy
  • select_related: list of names of related models fetched from database :type select_related: List
  • related_models: list or dict of related models :type related_models: Union[List, Dict]
  • related_field: field with relation declaration :type related_field: ForeignKeyField

Returns:

returns model if model is populated from database :rtype: Optional[Model]

_process_table_prefix

1
2
 | @classmethod
 | _process_table_prefix(cls, source_model: Type["Model"], current_relation_str: str, related_field: "ForeignKeyField", used_prefixes: List[str]) -> str

Arguments:

  • source_model: model on which relation was defined :type source_model: Type[Model]
  • current_relation_str: current relation string :type current_relation_str: str
  • related_field: field with relation declaration :type related_field: "ForeignKeyField"
  • used_prefixes: list of already extracted prefixes :type used_prefixes: List[str]

Returns:

table_prefix to use :rtype: str

_populate_nested_models_from_row

1
2
 | @classmethod
 | _populate_nested_models_from_row(cls, item: dict, row: ResultProxy, source_model: Type["Model"], related_models: Any, excludable: ExcludableItems, table_prefix: str, used_prefixes: List[str], current_relation_str: str = None, proxy_source_model: Type["Model"] = None) -> dict

Traverses structure of related models and populates the nested models from the database row. Related models can be a list if only directly related models are to be populated, converted to dict if related models also have their own related models to be populated.

Recurrently calls from_row method on nested instances and create nested instances. In the end those instances are added to the final model dictionary.

Arguments:

  • proxy_source_model: source model from which querysetproxy is constructed :type proxy_source_model: Optional[Type["ModelRow"]]
  • excludable: structure of fields to include and exclude :type excludable: ExcludableItems
  • source_model: source model from which relation started :type source_model: Type[Model]
  • current_relation_str: joined related parts into one string :type current_relation_str: str
  • item: dictionary of already populated nested models, otherwise empty dict :type item: Dict
  • row: raw result row from the database :type row: ResultProxy
  • related_models: list or dict of related models :type related_models: Union[Dict, List]

Returns:

dictionary with keys corresponding to model fields names and values are database values :rtype: Dict

_process_remainder_and_relation_string

1
2
 | @staticmethod
 | _process_remainder_and_relation_string(related_models: Union[Dict, List], current_relation_str: Optional[str], related: str) -> Tuple[str, Optional[Union[Dict, List]]]

Process remainder models and relation string

Arguments:

  • related_models: list or dict of related models :type related_models: Union[Dict, List]
  • current_relation_str: current relation string :type current_relation_str: Optional[str]
  • related: name of the relation :type related: str

_populate_through_instance

1
2
 | @classmethod
 | _populate_through_instance(cls, row: ResultProxy, item: Dict, related: str, excludable: ExcludableItems, child: "Model", proxy_source_model: Optional[Type["Model"]]) -> None

Populates the through model on reverse side of current query. Normally it's child class, unless the query is from queryset.

Arguments:

  • row: row from db result :type row: ResultProxy
  • item: parent item dict :type item: Dict
  • related: current relation name :type related: str
  • excludable: structure of fields to include and exclude :type excludable: ExcludableItems
  • child: child item of parent :type child: "Model"
  • proxy_source_model: source model from which querysetproxy is constructed :type proxy_source_model: Type["Model"]

_create_through_instance

1
2
 | @classmethod
 | _create_through_instance(cls, row: ResultProxy, through_name: str, related: str, excludable: ExcludableItems) -> "ModelRow"

Initialize the through model from db row. Excluded all relation fields and other exclude/include set in excludable.

Arguments:

  • row: loaded row from database :type row: sqlalchemy.engine.ResultProxy
  • through_name: name of the through field :type through_name: str
  • related: name of the relation :type related: str
  • excludable: structure of fields to include and exclude :type excludable: ExcludableItems

Returns:

initialized through model without relation :rtype: "ModelRow"

extract_prefixed_table_columns

1
2
 | @classmethod
 | extract_prefixed_table_columns(cls, item: dict, row: ResultProxy, table_prefix: str, excludable: ExcludableItems) -> Dict

Extracts own fields from raw sql result, using a given prefix. Prefix changes depending on the table's position in a join.

If the table is a main table, there is no prefix. All joined tables have prefixes to allow duplicate column names, as well as duplicated joins to the same table from multiple different tables.

Extracted fields populates the related dict later used to construct a Model.

Used in Model.from_row and PrefetchQuery._populate_rows methods.

Arguments:

  • excludable: structure of fields to include and exclude :type excludable: ExcludableItems
  • item: dictionary of already populated nested models, otherwise empty dict :type item: Dict
  • row: raw result row from the database :type row: sqlalchemy.engine.result.ResultProxy
  • table_prefix: prefix of the table from AliasManager each pair of tables have own prefix (two of them depending on direction) - used in joins to allow multiple joins to the same table. :type table_prefix: str

Returns:

dictionary with keys corresponding to model fields names and values are database values :rtype: Dict