Skip to content

queryset.clause

FilterGroup Objects

1
class FilterGroup()

Filter groups are used in complex queries condition to group and and or clauses in where condition

resolve

1
 | resolve(model_cls: Type["Model"], select_related: List = None, filter_clauses: List = None) -> Tuple[List[FilterAction], List[str]]

Resolves the FilterGroups actions to use proper target model, replace complex relation prefixes if needed and nested groups also resolved.

Arguments:

  • model_cls: model from which the query is run :type model_cls: Type["Model"]
  • select_related: list of models to join :type select_related: List[str]
  • filter_clauses: list of filter conditions :type filter_clauses: List[FilterAction]

Returns:

list of filter conditions and select_related list :rtype: Tuple[List[FilterAction], List[str]]

_iter

1
 | _iter() -> Generator

Iterates all actions in a tree

Returns:

generator yielding from own actions and nested groups :rtype: Generator

_get_text_clauses

1
 | _get_text_clauses() -> List[sqlalchemy.sql.expression.TextClause]

Helper to return list of text queries from actions and nested groups

Returns:

list of text queries from actions and nested groups :rtype: List[sqlalchemy.sql.elements.TextClause]

get_text_clause

1
 | get_text_clause() -> sqlalchemy.sql.expression.TextClause

Returns all own actions and nested groups conditions compiled and joined inside parentheses. Escapes characters if it's required. Substitutes values of the models if value is a ormar Model with its pk value. Compiles the clause.

Returns:

complied and escaped clause :rtype: sqlalchemy.sql.elements.TextClause

or_

1
or_(*args: FilterGroup, **kwargs: Any) -> FilterGroup

Construct or filter from nested groups and keyword arguments

Arguments:

  • args: nested filter groups :type args: Tuple[FilterGroup]
  • kwargs: fields names and proper value types :type kwargs: Any

Returns:

FilterGroup ready to be resolved :rtype: ormar.queryset.clause.FilterGroup

and_

1
and_(*args: FilterGroup, **kwargs: Any) -> FilterGroup

Construct and filter from nested groups and keyword arguments

Arguments:

  • args: nested filter groups :type args: Tuple[FilterGroup]
  • kwargs: fields names and proper value types :type kwargs: Any

Returns:

FilterGroup ready to be resolved :rtype: ormar.queryset.clause.FilterGroup

QueryClause Objects

1
class QueryClause()

Constructs FilterActions from strings passed as arguments

prepare_filter

1
 | prepare_filter(_own_only: bool = False, **kwargs: Any) -> Tuple[List[FilterAction], List[str]]

Main external access point that processes the clauses into sqlalchemy text clauses and updates select_related list with implicit related tables mentioned in select_related strings but not included in select_related.

Arguments:

  • _own_only: :type _own_only:
  • kwargs: key, value pair with column names and values :type kwargs: Any

Returns:

Tuple with list of where clauses and updated select_related list :rtype: Tuple[List[sqlalchemy.sql.elements.TextClause], List[str]]

_populate_filter_clauses

1
 | _populate_filter_clauses(_own_only: bool, **kwargs: Any) -> Tuple[List[FilterAction], List[str]]

Iterates all clauses and extracts used operator and field from related models if needed. Based on the chain of related names the target table is determined and the final clause is escaped if needed and compiled.

Arguments:

  • kwargs: key, value pair with column names and values :type kwargs: Any

Returns:

Tuple with list of where clauses and updated select_related list :rtype: Tuple[List[sqlalchemy.sql.elements.TextClause], List[str]]

_register_complex_duplicates

1
 | _register_complex_duplicates(select_related: List[str]) -> None

Checks if duplicate aliases are presented which can happen in self relation or when two joins end with the same pair of models.

If there are duplicates, the all duplicated joins are registered as source model and whole relation key (not just last relation name).

Arguments:

  • select_related: list of relation strings :type select_related: List[str]

Returns:

None :rtype: None

1
 | _parse_related_prefixes(select_related: List[str]) -> List[Prefix]

Walks all relation strings and parses the target models and prefixes.

Arguments:

  • select_related: list of relation strings :type select_related: List[str]

Returns:

list of parsed prefixes :rtype: List[Prefix]

_switch_filter_action_prefixes

1
 | _switch_filter_action_prefixes(filter_clauses: List[FilterAction]) -> List[FilterAction]

Substitutes aliases for filter action if the complex key (whole relation str) is present in alias_manager.

Arguments:

  • filter_clauses: raw list of actions :type filter_clauses: List[FilterAction]

Returns:

list of actions with aliases changed if needed :rtype: List[FilterAction]

_verify_prefix_and_switch

1
 | _verify_prefix_and_switch(action: "FilterAction") -> None

Helper to switch prefix to complex relation one if required

Arguments:

  • action: action to switch prefix in :type action: ormar.queryset.actions.filter_action.FilterAction