Skip to content

queryset.reverse_alias_resolver

ReverseAliasResolver Objects

1
class ReverseAliasResolver()

Class is used to reverse resolve table aliases into relation strings to parse raw data columns and replace table prefixes with full relation string

resolve_columns

1
 | resolve_columns(columns_names: List[str]) -> Dict

Takes raw query prefixed column and resolves the prefixes to relation strings (relation names connected with dunders).

Arguments:

  • columns_names: list of column names with prefixes from query :type columns_names: List[str]

Returns:

dictionary of prefix: resolved names :rtype: Union[None, Dict[str, str]]

_resolve_column_with_prefix

1
 | _resolve_column_with_prefix(column_name: str, prefix: str) -> None

Takes the prefixed column, checks if field should be excluded, and if not it proceeds to replace prefix of a table with full relation string.

Sample: translates: "xsd12df_name" -> into: "posts__user__name"

Arguments:

  • column_name: prefixed name of the column :type column_name: str
  • prefix: extracted prefix :type prefix: str

_check_if_field_is_excluded

1
 | _check_if_field_is_excluded(prefix: str, field: "ForeignKeyField", is_through: bool) -> bool

Checks if given relation is excluded in current query.

Note that in contrary to other queryset methods here you can exclude the in-between models but keep the end columns, which does not make sense when parsing the raw data into models.

So in relation category -> category_x_post -> post -> user you can exclude category_x_post and post models but can keep the user one. (in ormar model context that is not possible as if you would exclude through and post model there would be no way to reach user model).

Exclusions happen on a model before the current one, so we need to move back in chain of model by one or by two (m2m relations have through model in between)

Arguments:

  • prefix: table alias :type prefix: str
  • field: field with relation :type field: ForeignKeyField
  • is_through: flag if current table is a through table :type is_through: bool

Returns:

result of the check :rtype: bool

_get_previous_excludable

1
 | _get_previous_excludable(prefix: str, field: "ForeignKeyField", shift: int = 1) -> "Excludable"

Returns excludable related to model previous in chain of models. Used to check if current model should be excluded.

Arguments:

  • prefix: prefix of a current table :type prefix: str
  • field: field with relation :type field: ForeignKeyField
  • shift: how many model back to go - for m2m it's 2 due to through models :type shift: int

Returns:

excludable for previous model :rtype: Excludable

_create_prefixes_map

1
 | _create_prefixes_map() -> None

Creates a map of alias manager aliases keys to relation strings. I.e in alias manager you can have alias user_roles: xas12ad

This method will create entry user_roles: roles, where roles is a name of relation on user model.

Will also keep the relation field in separate dictionary so we can later extract field names and owner models.

_handle_through_fields_and_prefix

1
 | _handle_through_fields_and_prefix(model_cls: Type["Model"], field: "ForeignKeyField", previous_related_str: str, relation: str) -> str

Registers through models for m2m relations and switches prefix for the one linking from through model to target model.

For other relations returns current model name + relation name as prefix. Nested relations are a chain of relation names with __ in between.

Arguments:

  • model_cls: model of current relation :type model_cls: Type["Model"]
  • field: field with relation :type field: ForeignKeyField
  • previous_related_str: concatenated chain linked with "__" :type previous_related_str: str
  • relation: name of the current relation in chain :type relation: str

Returns:

name of prefix to populate :rtype: str