Files
ormar/docs/api/query-set/utils.md

4.9 KiB

queryset.utils

check_node_not_dict_or_not_last_node

check_node_not_dict_or_not_last_node(part: str, parts: List, current_level: Any) -> bool

Checks if given name is not present in the current level of the structure. Checks if given name is not the last name in the split list of parts. Checks if the given name in current level is not a dictionary.

All those checks verify if there is a need for deeper traversal.

Arguments:

  • part (str):
  • parts (List[str]):
  • current_level (Any): current level of the traversed structure

Returns:

(bool): result of the check

translate_list_to_dict

translate_list_to_dict(list_to_trans: Union[List, Set], is_order: bool = False) -> Dict

Splits the list of strings by '__' and converts them to dictionary with nested models grouped by parent model. That way each model appears only once in the whole dictionary and children are grouped under parent name.

Default required key ise Ellipsis like in pydantic.

Arguments:

  • list_to_trans (set): input list
  • is_order (bool): flag if change affects order_by clauses are they require special default value with sort order.

Returns:

(Dict): converted to dictionary input list

convert_set_to_required_dict

convert_set_to_required_dict(set_to_convert: set) -> Dict

Converts set to dictionary of required keys. Required key is Ellipsis.

Arguments:

  • set_to_convert (set): set to convert to dict

Returns:

(Dict): set converted to dict of ellipsis

update

update(current_dict: Any, updating_dict: Any) -> Dict

Update one dict with another but with regard for nested keys.

That way nested sets are unionised, dicts updated and only other values are overwritten.

Arguments:

  • current_dict (Dict[str, ellipsis]): dict to update
  • updating_dict (Dict): dict with values to update

Returns:

(Dict): combination of both dicts

update_dict_from_list

update_dict_from_list(curr_dict: Dict, list_to_update: Union[List, Set]) -> Dict

Converts the list into dictionary and later performs special update, where nested keys that are sets or dicts are combined and not overwritten.

Arguments:

  • curr_dict (Dict): dict to update
  • list_to_update (List[str]): list with values to update the dict

Returns:

(Dict): updated dict

extract_nested_models

extract_nested_models(model: "Model", model_type: Type["Model"], select_dict: Dict, extracted: Dict) -> None

Iterates over model relations and extracts all nested models from select_dict and puts them in corresponding list under relation name in extracted dict.keys

Basically flattens all relation to dictionary of all related models, that can be used on several models and extract all of their children into dictionary of lists witch children models.

Goes also into nested relations if needed (specified in select_dict).

Arguments:

  • model (Model): parent Model
  • model_type (Type[Model]): parent model class
  • select_dict (Dict): dictionary of related models from select_related
  • extracted (Dict): dictionary with already extracted models

extract_models_to_dict_of_lists

extract_models_to_dict_of_lists(model_type: Type["Model"], models: Sequence["Model"], select_dict: Dict, extracted: Dict = None) -> Dict

Receives a list of models and extracts all of the children and their children into dictionary of lists with children models, flattening the structure to one dict with all children models under their relation keys.

Arguments:

  • model_type (Type[Model]): parent model class
  • models (List[Model]): list of models from which related models should be extracted.
  • select_dict (Dict): dictionary of related models from select_related
  • extracted (Dict): dictionary with already extracted models

Returns:

(Dict): dictionary of lists f related models

get_relationship_alias_model_and_str

get_relationship_alias_model_and_str(source_model: Type["Model"], related_parts: List) -> Tuple[str, Type["Model"], str, bool]

Walks the relation to retrieve the actual model on which the clause should be constructed, extracts alias based on last relation leading to target model.

Arguments:

  • related_parts (Union[List, List[str]]): list of related names extracted from string
  • source_model (Type[Model]): model from which relation starts

Returns:

(Tuple[str, Type["Model"], str]): table prefix, target model and relation string