Skip to content

queryset.utils

check_node_not_dict_or_not_last_node

1
check_node_not_dict_or_not_last_node(part: str, is_last: bool, 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: :type part: str
  • is_last: flag to check if last element :type is_last: bool
  • current_level: current level of the traversed structure :type current_level: Any

Returns:

result of the check :rtype: bool

translate_list_to_dict

1
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: input list :type list_to_trans: Union[List, Set]
  • is_order: flag if change affects order_by clauses are they require special default value with sort order. :type is_order: bool

Returns:

converted to dictionary input list :rtype: Dict

convert_set_to_required_dict

1
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 to convert to dict :type set_to_convert: set

Returns:

set converted to dict of ellipsis :rtype: Dict

update

1
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 to update :type current_dict: Dict[str, ellipsis]
  • updating_dict: dict with values to update :type updating_dict: Dict

Returns:

combination of both dicts :rtype: Dict

subtract_dict

1
subtract_dict(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 to update :type current_dict: Dict[str, ellipsis]
  • updating_dict: dict with values to update :type updating_dict: Dict

Returns:

combination of both dicts :rtype: Dict

update_dict_from_list

1
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 to update :type curr_dict: Dict
  • list_to_update: list with values to update the dict :type list_to_update: List[str]

Returns:

updated dict :rtype: Dict

extract_nested_models

1
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: parent Model :type model: Model
  • model_type: parent model class :type model_type: Type[Model]
  • select_dict: dictionary of related models from select_related :type select_dict: Dict
  • extracted: dictionary with already extracted models :type extracted: Dict

extract_models_to_dict_of_lists

1
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: parent model class :type model_type: Type[Model]
  • models: list of models from which related models should be extracted. :type models: List[Model]
  • select_dict: dictionary of related models from select_related :type select_dict: Dict
  • extracted: dictionary with already extracted models :type extracted: Dict

Returns:

dictionary of lists f related models :rtype: Dict

get_relationship_alias_model_and_str

1
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: list of related names extracted from string :type related_parts: Union[List, List[str]]
  • source_model: model from which relation starts :type source_model: Type[Model]

Returns:

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

_process_through_field

1
_process_through_field(related_parts: List, relation: Optional[str], related_field: "BaseField", previous_model: Type["Model"], previous_models: List[Type["Model"]]) -> Tuple[Type["Model"], Optional[str], bool]

Helper processing through models as they need to be treated differently.

Arguments:

  • related_parts: split relation string :type related_parts: List[str]
  • relation: relation name :type relation: str
  • related_field: field with relation declaration :type related_field: "ForeignKeyField"
  • previous_model: model from which relation is coming :type previous_model: Type["Model"]
  • previous_models: list of already visited models in relation chain :type previous_models: List[Type["Model"]]

Returns:

previous_model, relation, is_through :rtype: Tuple[Type["Model"], str, bool]