next part of the docs and api documentation in beta ver
This commit is contained in:
174
docs/api/query-set/clause.md
Normal file
174
docs/api/query-set/clause.md
Normal file
@ -0,0 +1,174 @@
|
||||
<a name="queryset.clause"></a>
|
||||
# queryset.clause
|
||||
|
||||
<a name="queryset.clause.FILTER_OPERATORS"></a>
|
||||
#### FILTER\_OPERATORS
|
||||
|
||||
<a name="queryset.clause.ESCAPE_CHARACTERS"></a>
|
||||
#### ESCAPE\_CHARACTERS
|
||||
|
||||
<a name="queryset.clause.QueryClause"></a>
|
||||
## QueryClause Objects
|
||||
|
||||
```python
|
||||
class QueryClause()
|
||||
```
|
||||
|
||||
Constructs where clauses from strings passed as arguments
|
||||
|
||||
<a name="queryset.clause.QueryClause.__init__"></a>
|
||||
#### \_\_init\_\_
|
||||
|
||||
```python
|
||||
| __init__(model_cls: Type["Model"], filter_clauses: List, select_related: List) -> None
|
||||
```
|
||||
|
||||
<a name="queryset.clause.QueryClause.filter"></a>
|
||||
#### filter
|
||||
|
||||
```python
|
||||
| filter(**kwargs: Any) -> Tuple[List[sqlalchemy.sql.expression.TextClause], 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**:
|
||||
|
||||
- `kwargs (Any)`: key, value pair with column names and values
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Tuple[List[sqlalchemy.sql.elements.TextClause], List[str]])`: Tuple with list of where clauses and updated select_related list
|
||||
|
||||
<a name="queryset.clause.QueryClause._populate_filter_clauses"></a>
|
||||
#### \_populate\_filter\_clauses
|
||||
|
||||
```python
|
||||
| _populate_filter_clauses(**kwargs: Any) -> Tuple[List[sqlalchemy.sql.expression.TextClause], 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 (Any)`: key, value pair with column names and values
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Tuple[List[sqlalchemy.sql.elements.TextClause], List[str]])`: Tuple with list of where clauses and updated select_related list
|
||||
|
||||
<a name="queryset.clause.QueryClause._process_column_clause_for_operator_and_value"></a>
|
||||
#### \_process\_column\_clause\_for\_operator\_and\_value
|
||||
|
||||
```python
|
||||
| _process_column_clause_for_operator_and_value(value: Any, op: str, column: sqlalchemy.Column, table: sqlalchemy.Table, table_prefix: str) -> sqlalchemy.sql.expression.TextClause
|
||||
```
|
||||
|
||||
Escapes characters if it's required.
|
||||
Substitutes values of the models if value is a ormar Model with its pk value.
|
||||
Compiles the clause.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `value (Any)`: value of the filter
|
||||
- `op (str)`: filter operator
|
||||
- `column (sqlalchemy.sql.schema.Column)`: column on which filter should be applied
|
||||
- `table (sqlalchemy.sql.schema.Table)`: table on which filter should be applied
|
||||
- `table_prefix (str)`: prefix from AliasManager
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(sqlalchemy.sql.elements.TextClause)`: complied and escaped clause
|
||||
|
||||
<a name="queryset.clause.QueryClause._determine_filter_target_table"></a>
|
||||
#### \_determine\_filter\_target\_table
|
||||
|
||||
```python
|
||||
| _determine_filter_target_table(related_parts: List[str], select_related: List[str]) -> Tuple[List[str], str, Type["Model"]]
|
||||
```
|
||||
|
||||
Adds related strings to select_related list otherwise the clause would fail as
|
||||
the required columns would not be present. That means that select_related
|
||||
list is filled with missing values present in filters.
|
||||
|
||||
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[str])`: list of split parts of related string
|
||||
- `select_related (List[str])`: list of related models
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Tuple[List[str], str, Type[Model]])`: list of related models, table_prefix, final model class
|
||||
|
||||
<a name="queryset.clause.QueryClause._compile_clause"></a>
|
||||
#### \_compile\_clause
|
||||
|
||||
```python
|
||||
| _compile_clause(clause: sqlalchemy.sql.expression.BinaryExpression, column: sqlalchemy.Column, table: sqlalchemy.Table, table_prefix: str, modifiers: Dict) -> sqlalchemy.sql.expression.TextClause
|
||||
```
|
||||
|
||||
Compiles the clause to str using appropriate database dialect, replace columns
|
||||
names with aliased names and converts it back to TextClause.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `clause (sqlalchemy.sql.elements.BinaryExpression)`: original not compiled clause
|
||||
- `column (sqlalchemy.sql.schema.Column)`: column on which filter should be applied
|
||||
- `table (sqlalchemy.sql.schema.Table)`: table on which filter should be applied
|
||||
- `table_prefix (str)`: prefix from AliasManager
|
||||
- `modifiers (Dict[str, NoneType])`: sqlalchemy modifiers - used only to escape chars here
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(sqlalchemy.sql.elements.TextClause)`: compiled and escaped clause
|
||||
|
||||
<a name="queryset.clause.QueryClause._escape_characters_in_clause"></a>
|
||||
#### \_escape\_characters\_in\_clause
|
||||
|
||||
```python
|
||||
| @staticmethod
|
||||
| _escape_characters_in_clause(op: str, value: Any) -> Tuple[Any, bool]
|
||||
```
|
||||
|
||||
Escapes the special characters ["%", "_"] if needed.
|
||||
Adds `%` for `like` queries.
|
||||
|
||||
**Raises**:
|
||||
|
||||
- `QueryDefinitionError`: if contains or icontains is used with
|
||||
ormar model instance
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `op (str)`: operator used in query
|
||||
- `value (Any)`: value of the filter
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Tuple[Any, bool])`: escaped value and flag if escaping is needed
|
||||
|
||||
<a name="queryset.clause.QueryClause._extract_operator_field_and_related"></a>
|
||||
#### \_extract\_operator\_field\_and\_related
|
||||
|
||||
```python
|
||||
| @staticmethod
|
||||
| _extract_operator_field_and_related(parts: List[str]) -> Tuple[str, str, Optional[List]]
|
||||
```
|
||||
|
||||
Splits filter query key and extracts required parts.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `parts (List[str])`: split filter query key
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Tuple[str, str, Optional[List]])`: operator, field_name, list of related parts
|
||||
|
||||
36
docs/api/query-set/filter-query.md
Normal file
36
docs/api/query-set/filter-query.md
Normal file
@ -0,0 +1,36 @@
|
||||
<a name="queryset.filter_query"></a>
|
||||
# queryset.filter\_query
|
||||
|
||||
<a name="queryset.filter_query.FilterQuery"></a>
|
||||
## FilterQuery Objects
|
||||
|
||||
```python
|
||||
class FilterQuery()
|
||||
```
|
||||
|
||||
Modifies the select query with given list of where/filter clauses.
|
||||
|
||||
<a name="queryset.filter_query.FilterQuery.__init__"></a>
|
||||
#### \_\_init\_\_
|
||||
|
||||
```python
|
||||
| __init__(filter_clauses: List, exclude: bool = False) -> None
|
||||
```
|
||||
|
||||
<a name="queryset.filter_query.FilterQuery.apply"></a>
|
||||
#### apply
|
||||
|
||||
```python
|
||||
| apply(expr: sqlalchemy.sql.select) -> sqlalchemy.sql.select
|
||||
```
|
||||
|
||||
Applies all filter clauses if set.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `expr (sqlalchemy.sql.selectable.Select)`: query to modify
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(sqlalchemy.sql.selectable.Select)`: modified query
|
||||
|
||||
262
docs/api/query-set/join.md
Normal file
262
docs/api/query-set/join.md
Normal file
@ -0,0 +1,262 @@
|
||||
<a name="queryset.join"></a>
|
||||
# queryset.join
|
||||
|
||||
<a name="queryset.join.JoinParameters"></a>
|
||||
## JoinParameters Objects
|
||||
|
||||
```python
|
||||
class JoinParameters(NamedTuple)
|
||||
```
|
||||
|
||||
Named tuple that holds set of parameters passed during join construction.
|
||||
|
||||
<a name="queryset.join.JoinParameters.prev_model"></a>
|
||||
#### prev\_model
|
||||
|
||||
<a name="queryset.join.JoinParameters.previous_alias"></a>
|
||||
#### previous\_alias
|
||||
|
||||
<a name="queryset.join.JoinParameters.from_table"></a>
|
||||
#### from\_table
|
||||
|
||||
<a name="queryset.join.JoinParameters.model_cls"></a>
|
||||
#### model\_cls
|
||||
|
||||
<a name="queryset.join.SqlJoin"></a>
|
||||
## SqlJoin Objects
|
||||
|
||||
```python
|
||||
class SqlJoin()
|
||||
```
|
||||
|
||||
<a name="queryset.join.SqlJoin.__init__"></a>
|
||||
#### \_\_init\_\_
|
||||
|
||||
```python
|
||||
| __init__(used_aliases: List, select_from: sqlalchemy.sql.select, columns: List[sqlalchemy.Column], fields: Optional[Union[Set, Dict]], exclude_fields: Optional[Union[Set, Dict]], order_columns: Optional[List], sorted_orders: OrderedDict) -> None
|
||||
```
|
||||
|
||||
<a name="queryset.join.SqlJoin.alias_manager"></a>
|
||||
#### alias\_manager
|
||||
|
||||
```python
|
||||
| @staticmethod
|
||||
| alias_manager(model_cls: Type["Model"]) -> AliasManager
|
||||
```
|
||||
|
||||
Shortcut for ormars model AliasManager stored on Meta.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `model_cls (Type[Model])`: ormar Model class
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(AliasManager)`: alias manager from model's Meta
|
||||
|
||||
<a name="queryset.join.SqlJoin.on_clause"></a>
|
||||
#### on\_clause
|
||||
|
||||
```python
|
||||
| @staticmethod
|
||||
| on_clause(previous_alias: str, alias: str, from_clause: str, to_clause: str) -> text
|
||||
```
|
||||
|
||||
Receives aliases and names of both ends of the join and combines them
|
||||
into one text clause used in joins.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `previous_alias (str)`: alias of previous table
|
||||
- `alias (str)`: alias of current table
|
||||
- `from_clause (str)`: from table name
|
||||
- `to_clause (str)`: to table name
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(sqlalchemy.text)`: clause combining all strings
|
||||
|
||||
<a name="queryset.join.SqlJoin.update_inclusions"></a>
|
||||
#### update\_inclusions
|
||||
|
||||
```python
|
||||
| @staticmethod
|
||||
| update_inclusions(model_cls: Type["Model"], fields: Optional[Union[Set, Dict]], exclude_fields: Optional[Union[Set, Dict]], nested_name: str) -> Tuple[Optional[Union[Dict, Set]], Optional[Union[Dict, Set]]]
|
||||
```
|
||||
|
||||
Extract nested fields and exclude_fields if applicable.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `model_cls (Type["Model"])`: ormar model class
|
||||
- `fields (Optional[Union[Set, Dict]])`: fields to include
|
||||
- `exclude_fields (Optional[Union[Set, Dict]])`: fields to exclude
|
||||
- `nested_name (str)`: name of the nested field
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Tuple[Optional[Union[Dict, Set]], Optional[Union[Dict, Set]]])`: updated exclude and include fields from nested objects
|
||||
|
||||
<a name="queryset.join.SqlJoin.build_join"></a>
|
||||
#### build\_join
|
||||
|
||||
```python
|
||||
| build_join(item: str, join_parameters: JoinParameters) -> Tuple[List, sqlalchemy.sql.select, List, OrderedDict]
|
||||
```
|
||||
|
||||
Main external access point for building a join.
|
||||
Splits the join definition, updates fields and exclude_fields if needed,
|
||||
handles switching to through models for m2m relations, returns updated lists of
|
||||
used_aliases and sort_orders.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `item (str)`: string with join definition
|
||||
- `join_parameters (JoinParameters)`: parameters from previous/ current join
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Tuple[List[str], Join, List[TextClause], collections.OrderedDict])`: list of used aliases, select from, list of aliased columns, sort orders
|
||||
|
||||
<a name="queryset.join.SqlJoin._build_join_parameters"></a>
|
||||
#### \_build\_join\_parameters
|
||||
|
||||
```python
|
||||
| _build_join_parameters(part: str, join_params: JoinParameters, fields: Optional[Union[Set, Dict]], exclude_fields: Optional[Union[Set, Dict]], is_multi: bool = False) -> JoinParameters
|
||||
```
|
||||
|
||||
Updates used_aliases to not join multiple times to the same table.
|
||||
Updates join parameters with new values.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `part (str)`: part of the join str definition
|
||||
- `join_params (JoinParameters)`: parameters from previous/ current join
|
||||
- `fields (Optional[Union[Set, Dict]])`: fields to include
|
||||
- `exclude_fields (Optional[Union[Set, Dict]])`: fields to exclude
|
||||
- `is_multi (bool)`: flag if the relation is m2m
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(ormar.queryset.join.JoinParameters)`: updated join parameters
|
||||
|
||||
<a name="queryset.join.SqlJoin._process_join"></a>
|
||||
#### \_process\_join
|
||||
|
||||
```python
|
||||
| _process_join(join_params: JoinParameters, is_multi: bool, model_cls: Type["Model"], part: str, alias: str, fields: Optional[Union[Set, Dict]], exclude_fields: Optional[Union[Set, Dict]]) -> None
|
||||
```
|
||||
|
||||
Resolves to and from column names and table names.
|
||||
|
||||
Produces on_clause.
|
||||
|
||||
Performs actual join updating select_from parameter.
|
||||
|
||||
Adds aliases of required column to list of columns to include in query.
|
||||
|
||||
Updates the used aliases list directly.
|
||||
|
||||
Process order_by causes for non m2m relations.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `join_params (JoinParameters)`: parameters from previous/ current join
|
||||
- `is_multi (bool)`: flag if it's m2m relation
|
||||
- `model_cls (ormar.models.metaclass.ModelMetaclass)`:
|
||||
- `part (str)`: name of the field used in join
|
||||
- `alias (str)`: alias of the current join
|
||||
- `fields (Optional[Union[Set, Dict]])`: fields to include
|
||||
- `exclude_fields (Optional[Union[Set, Dict]])`: fields to exclude
|
||||
|
||||
<a name="queryset.join.SqlJoin._switch_many_to_many_order_columns"></a>
|
||||
#### \_switch\_many\_to\_many\_order\_columns
|
||||
|
||||
```python
|
||||
| _switch_many_to_many_order_columns(part: str, new_part: str) -> None
|
||||
```
|
||||
|
||||
Substitutes the name of the relation with actual model name in m2m order bys.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `part (str)`: name of the field with relation
|
||||
- `new_part (str)`: name of the target model
|
||||
|
||||
<a name="queryset.join.SqlJoin._check_if_condition_apply"></a>
|
||||
#### \_check\_if\_condition\_apply
|
||||
|
||||
```python
|
||||
| @staticmethod
|
||||
| _check_if_condition_apply(condition: List, part: str) -> bool
|
||||
```
|
||||
|
||||
Checks filter conditions to find if they apply to current join.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `condition (List[str])`: list of parts of condition split by '__'
|
||||
- `part (str)`: name of the current relation join.
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(bool)`: result of the check
|
||||
|
||||
<a name="queryset.join.SqlJoin.set_aliased_order_by"></a>
|
||||
#### set\_aliased\_order\_by
|
||||
|
||||
```python
|
||||
| set_aliased_order_by(condition: List[str], alias: str, to_table: str, model_cls: Type["Model"]) -> None
|
||||
```
|
||||
|
||||
Substitute hyphens ('-') with descending order.
|
||||
Construct actual sqlalchemy text clause using aliased table and column name.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `condition (List[str])`: list of parts of a current condition split by '__'
|
||||
- `alias (str)`: alias of the table in current join
|
||||
- `to_table (sqlalchemy.sql.elements.quoted_name)`: target table
|
||||
- `model_cls (ormar.models.metaclass.ModelMetaclass)`: ormar model class
|
||||
|
||||
<a name="queryset.join.SqlJoin.get_order_bys"></a>
|
||||
#### get\_order\_bys
|
||||
|
||||
```python
|
||||
| get_order_bys(alias: str, to_table: str, pkname_alias: str, part: str, model_cls: Type["Model"]) -> None
|
||||
```
|
||||
|
||||
Triggers construction of order bys if they are given.
|
||||
Otherwise by default each table is sorted by a primary key column asc.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `alias (str)`: alias of current table in join
|
||||
- `to_table (sqlalchemy.sql.elements.quoted_name)`: target table
|
||||
- `pkname_alias (str)`: alias of the primary key column
|
||||
- `part (str)`: name of the current relation join
|
||||
- `model_cls (Type[Model])`: ormar model class
|
||||
|
||||
<a name="queryset.join.SqlJoin.get_to_and_from_keys"></a>
|
||||
#### get\_to\_and\_from\_keys
|
||||
|
||||
```python
|
||||
| @staticmethod
|
||||
| get_to_and_from_keys(join_params: JoinParameters, is_multi: bool, model_cls: Type["Model"], part: str) -> Tuple[str, str]
|
||||
```
|
||||
|
||||
Based on the relation type, name of the relation and previous models and parts
|
||||
stored in JoinParameters it resolves the current to and from keys, which are
|
||||
different for ManyToMany relation, ForeignKey and reverse part of relations.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `join_params (JoinParameters)`: parameters from previous/ current join
|
||||
- `is_multi (bool)`: flag if the relation is of m2m type
|
||||
- `model_cls (Type[Model])`: ormar model class
|
||||
- `part (str)`: name of the current relation join
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Tuple[str, str])`: to key and from key
|
||||
|
||||
36
docs/api/query-set/limit-query.md
Normal file
36
docs/api/query-set/limit-query.md
Normal file
@ -0,0 +1,36 @@
|
||||
<a name="queryset.limit_query"></a>
|
||||
# queryset.limit\_query
|
||||
|
||||
<a name="queryset.limit_query.LimitQuery"></a>
|
||||
## LimitQuery Objects
|
||||
|
||||
```python
|
||||
class LimitQuery()
|
||||
```
|
||||
|
||||
Modifies the select query with limit clause.
|
||||
|
||||
<a name="queryset.limit_query.LimitQuery.__init__"></a>
|
||||
#### \_\_init\_\_
|
||||
|
||||
```python
|
||||
| __init__(limit_count: Optional[int]) -> None
|
||||
```
|
||||
|
||||
<a name="queryset.limit_query.LimitQuery.apply"></a>
|
||||
#### apply
|
||||
|
||||
```python
|
||||
| apply(expr: sqlalchemy.sql.select) -> sqlalchemy.sql.select
|
||||
```
|
||||
|
||||
Applies the limit clause.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `expr (sqlalchemy.sql.selectable.Select)`: query to modify
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(sqlalchemy.sql.selectable.Select)`: modified query
|
||||
|
||||
36
docs/api/query-set/offset-query.md
Normal file
36
docs/api/query-set/offset-query.md
Normal file
@ -0,0 +1,36 @@
|
||||
<a name="queryset.offset_query"></a>
|
||||
# queryset.offset\_query
|
||||
|
||||
<a name="queryset.offset_query.OffsetQuery"></a>
|
||||
## OffsetQuery Objects
|
||||
|
||||
```python
|
||||
class OffsetQuery()
|
||||
```
|
||||
|
||||
Modifies the select query with offset if set
|
||||
|
||||
<a name="queryset.offset_query.OffsetQuery.__init__"></a>
|
||||
#### \_\_init\_\_
|
||||
|
||||
```python
|
||||
| __init__(query_offset: Optional[int]) -> None
|
||||
```
|
||||
|
||||
<a name="queryset.offset_query.OffsetQuery.apply"></a>
|
||||
#### apply
|
||||
|
||||
```python
|
||||
| apply(expr: sqlalchemy.sql.select) -> sqlalchemy.sql.select
|
||||
```
|
||||
|
||||
Applies the offset clause.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `expr (sqlalchemy.sql.selectable.Select)`: query to modify
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(sqlalchemy.sql.selectable.Select)`: modified query
|
||||
|
||||
36
docs/api/query-set/order-query.md
Normal file
36
docs/api/query-set/order-query.md
Normal file
@ -0,0 +1,36 @@
|
||||
<a name="queryset.order_query"></a>
|
||||
# queryset.order\_query
|
||||
|
||||
<a name="queryset.order_query.OrderQuery"></a>
|
||||
## OrderQuery Objects
|
||||
|
||||
```python
|
||||
class OrderQuery()
|
||||
```
|
||||
|
||||
Modifies the select query with given list of order_by clauses.
|
||||
|
||||
<a name="queryset.order_query.OrderQuery.__init__"></a>
|
||||
#### \_\_init\_\_
|
||||
|
||||
```python
|
||||
| __init__(sorted_orders: Dict) -> None
|
||||
```
|
||||
|
||||
<a name="queryset.order_query.OrderQuery.apply"></a>
|
||||
#### apply
|
||||
|
||||
```python
|
||||
| apply(expr: sqlalchemy.sql.select) -> sqlalchemy.sql.select
|
||||
```
|
||||
|
||||
Applies all order_by clauses if set.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `expr (sqlalchemy.sql.selectable.Select)`: query to modify
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(sqlalchemy.sql.selectable.Select)`: modified query
|
||||
|
||||
352
docs/api/query-set/prefetch-query.md
Normal file
352
docs/api/query-set/prefetch-query.md
Normal file
@ -0,0 +1,352 @@
|
||||
<a name="queryset.prefetch_query"></a>
|
||||
# queryset.prefetch\_query
|
||||
|
||||
<a name="queryset.prefetch_query.add_relation_field_to_fields"></a>
|
||||
#### add\_relation\_field\_to\_fields
|
||||
|
||||
```python
|
||||
add_relation_field_to_fields(fields: Union[Set[Any], Dict[Any, Any], None], related_field_name: str) -> Union[Set[Any], Dict[Any, Any], None]
|
||||
```
|
||||
|
||||
Adds related field into fields to include as otherwise it would be skipped.
|
||||
Related field is added only if fields are already populated.
|
||||
Empty fields implies all fields.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `fields (Dict)`: Union[Set[Any], Dict[Any, Any], None]
|
||||
- `related_field_name (str)`: name of the field with relation
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Union[Set[Any], Dict[Any, Any], None])`: updated fields dict
|
||||
|
||||
<a name="queryset.prefetch_query.sort_models"></a>
|
||||
#### sort\_models
|
||||
|
||||
```python
|
||||
sort_models(models: List["Model"], orders_by: Dict) -> List["Model"]
|
||||
```
|
||||
|
||||
Since prefetch query gets all related models by ids the sorting needs to happen in
|
||||
python. Since by default models are already sorted by id here we resort only if
|
||||
order_by parameters was set.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `models (List[tests.test_prefetch_related.Division])`: list of models already fetched from db
|
||||
- `orders_by (Dict[str, str])`: order by dictionary
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(List[tests.test_prefetch_related.Division])`: sorted list of models
|
||||
|
||||
<a name="queryset.prefetch_query.set_children_on_model"></a>
|
||||
#### set\_children\_on\_model
|
||||
|
||||
```python
|
||||
set_children_on_model(model: "Model", related: str, children: Dict, model_id: int, models: Dict, orders_by: Dict) -> None
|
||||
```
|
||||
|
||||
Extract ids of child models by given relation id key value.
|
||||
|
||||
Based on those ids the actual children model instances are fetched from
|
||||
already fetched data.
|
||||
|
||||
If needed the child models are resorted according to passed orders_by dict.
|
||||
|
||||
Also relation is registered as each child is set as parent related field name value.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `model (Model)`: parent model instance
|
||||
- `related (str)`: name of the related field
|
||||
- `children (Dict[int, set])`: dictionary of children ids/ related field value
|
||||
- `model_id (int)`: id of the model on which children should be set
|
||||
- `models (Dict)`: dictionary of child models instances
|
||||
- `orders_by (Dict)`: order_by dictionary
|
||||
|
||||
<a name="queryset.prefetch_query.PrefetchQuery"></a>
|
||||
## PrefetchQuery Objects
|
||||
|
||||
```python
|
||||
class PrefetchQuery()
|
||||
```
|
||||
|
||||
Query used to fetch related models in subsequent queries.
|
||||
Each model is fetched only ones by the name of the relation.
|
||||
That means that for each prefetch_related entry next query is issued to database.
|
||||
|
||||
<a name="queryset.prefetch_query.PrefetchQuery.__init__"></a>
|
||||
#### \_\_init\_\_
|
||||
|
||||
```python
|
||||
| __init__(model_cls: Type["Model"], fields: Optional[Union[Dict, Set]], exclude_fields: Optional[Union[Dict, Set]], prefetch_related: List, select_related: List, orders_by: List) -> None
|
||||
```
|
||||
|
||||
<a name="queryset.prefetch_query.PrefetchQuery.prefetch_related"></a>
|
||||
#### prefetch\_related
|
||||
|
||||
```python
|
||||
| async prefetch_related(models: Sequence["Model"], rows: List) -> Sequence["Model"]
|
||||
```
|
||||
|
||||
Main entry point for prefetch_query.
|
||||
|
||||
Receives list of already initialized parent models with all children from
|
||||
select_related already populated. Receives also list of row sql result rows
|
||||
as it's quicker to extract ids that way instead of calling each model.
|
||||
|
||||
Returns list with related models already prefetched and set.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `models (List[Model])`: list of already instantiated models from main query
|
||||
- `rows (List[sqlalchemy.engine.result.RowProxy])`: row sql result of the main query before the prefetch
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(List[Model])`: list of models with children prefetched
|
||||
|
||||
<a name="queryset.prefetch_query.PrefetchQuery._extract_ids_from_raw_data"></a>
|
||||
#### \_extract\_ids\_from\_raw\_data
|
||||
|
||||
```python
|
||||
| _extract_ids_from_raw_data(parent_model: Type["Model"], column_name: str) -> Set
|
||||
```
|
||||
|
||||
Iterates over raw rows and extract id values of relation columns by using
|
||||
prefixed column name.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `parent_model (Type[Model])`: ormar model class
|
||||
- `column_name (str)`: name of the relation column which is a key column
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(set)`: set of ids of related model that should be extracted
|
||||
|
||||
<a name="queryset.prefetch_query.PrefetchQuery._extract_ids_from_preloaded_models"></a>
|
||||
#### \_extract\_ids\_from\_preloaded\_models
|
||||
|
||||
```python
|
||||
| _extract_ids_from_preloaded_models(parent_model: Type["Model"], column_name: str) -> Set
|
||||
```
|
||||
|
||||
Extracts relation ids from already populated models if they were included
|
||||
in the original query before.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `parent_model (Type["Model"])`: model from which related ids should be extracted
|
||||
- `column_name (str)`: name of the relation column which is a key column
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(set)`: set of ids of related model that should be extracted
|
||||
|
||||
<a name="queryset.prefetch_query.PrefetchQuery._extract_required_ids"></a>
|
||||
#### \_extract\_required\_ids
|
||||
|
||||
```python
|
||||
| _extract_required_ids(parent_model: Type["Model"], reverse: bool, related: str) -> Set
|
||||
```
|
||||
|
||||
Delegates extraction of the fields to either get ids from raw sql response
|
||||
or from already populated models.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `parent_model (Type["Model"])`: model from which related ids should be extracted
|
||||
- `reverse (bool)`: flag if the relation is reverse
|
||||
- `related (str)`: name of the field with relation
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(set)`: set of ids of related model that should be extracted
|
||||
|
||||
<a name="queryset.prefetch_query.PrefetchQuery._get_filter_for_prefetch"></a>
|
||||
#### \_get\_filter\_for\_prefetch
|
||||
|
||||
```python
|
||||
| _get_filter_for_prefetch(parent_model: Type["Model"], target_model: Type["Model"], reverse: bool, related: str) -> List
|
||||
```
|
||||
|
||||
Populates where clause with condition to return only models within the
|
||||
set of extracted ids.
|
||||
|
||||
If there are no ids for relation the empty list is returned.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `parent_model (Type["Model"])`: model from which related ids should be extracted
|
||||
- `target_model (Type["Model"])`: model to which relation leads to
|
||||
- `reverse (bool)`: flag if the relation is reverse
|
||||
- `related (str)`: name of the field with relation
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(List[sqlalchemy.sql.elements.TextClause])`:
|
||||
|
||||
<a name="queryset.prefetch_query.PrefetchQuery._populate_nested_related"></a>
|
||||
#### \_populate\_nested\_related
|
||||
|
||||
```python
|
||||
| _populate_nested_related(model: "Model", prefetch_dict: Dict, orders_by: Dict) -> "Model"
|
||||
```
|
||||
|
||||
Populates all related models children of parent model that are
|
||||
included in prefetch query.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `model (Model)`: ormar model instance
|
||||
- `prefetch_dict (Dict)`: dictionary of models to prefetch
|
||||
- `orders_by (Dict)`: dictionary of order bys
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Model)`: model with children populated
|
||||
|
||||
<a name="queryset.prefetch_query.PrefetchQuery._prefetch_related_models"></a>
|
||||
#### \_prefetch\_related\_models
|
||||
|
||||
```python
|
||||
| async _prefetch_related_models(models: Sequence["Model"], rows: List) -> Sequence["Model"]
|
||||
```
|
||||
|
||||
Main method of the query.
|
||||
|
||||
Translates select nad prefetch list into dictionaries to avoid querying the
|
||||
same related models multiple times.
|
||||
|
||||
Keeps the list of already extracted models.
|
||||
|
||||
Extracts the related models from the database and later populate all children
|
||||
on each of the parent models from list.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `models (List[Model])`: list of parent models from main query
|
||||
- `rows (List[sqlalchemy.engine.result.RowProxy])`: raw response from sql query
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(List[Model])`: list of models with prefetch children populated
|
||||
|
||||
<a name="queryset.prefetch_query.PrefetchQuery._extract_related_models"></a>
|
||||
#### \_extract\_related\_models
|
||||
|
||||
```python
|
||||
| async _extract_related_models(related: str, target_model: Type["Model"], prefetch_dict: Dict, select_dict: Dict, fields: Union[Set[Any], Dict[Any, Any], None], exclude_fields: Union[Set[Any], Dict[Any, Any], None], orders_by: Dict) -> None
|
||||
```
|
||||
|
||||
Constructs queries with required ids and extracts data with fields that should
|
||||
be included/excluded.
|
||||
|
||||
Runs the queries against the database and populated dictionaries with ids and
|
||||
with actual extracted children models.
|
||||
|
||||
Calls itself recurrently to extract deeper nested relations of related model.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `related (str)`: name of the relation
|
||||
- `target_model (Type[Model])`: model to which relation leads to
|
||||
- `prefetch_dict (Dict)`: prefetch related list converted into dictionary
|
||||
- `select_dict (Dict)`: select related list converted into dictionary
|
||||
- `fields (Union[Set[Any], Dict[Any, Any], None])`: fields to include
|
||||
- `exclude_fields (Union[Set[Any], Dict[Any, Any], None])`: fields to exclude
|
||||
- `orders_by (Dict)`: dictionary of order bys clauses
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(None)`: None
|
||||
|
||||
<a name="queryset.prefetch_query.PrefetchQuery._run_prefetch_query"></a>
|
||||
#### \_run\_prefetch\_query
|
||||
|
||||
```python
|
||||
| async _run_prefetch_query(target_field: Type["BaseField"], fields: Union[Set[Any], Dict[Any, Any], None], exclude_fields: Union[Set[Any], Dict[Any, Any], None], filter_clauses: List) -> Tuple[str, List]
|
||||
```
|
||||
|
||||
Actually runs the queries against the database and populates the raw response
|
||||
for given related model.
|
||||
|
||||
Returns table prefix as it's later needed to eventually initialize the children
|
||||
models.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `target_field (Type["BaseField"])`: ormar field with relation definition
|
||||
- `fields (Union[Set[Any], Dict[Any, Any], None])`: fields to include
|
||||
- `exclude_fields (Union[Set[Any], Dict[Any, Any], None])`: fields to exclude
|
||||
- `filter_clauses (List[sqlalchemy.sql.elements.TextClause])`: list of clauses, actually one clause with ids of relation
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Tuple[str, List])`: table prefix and raw rows from sql response
|
||||
|
||||
<a name="queryset.prefetch_query.PrefetchQuery._get_select_related_if_apply"></a>
|
||||
#### \_get\_select\_related\_if\_apply
|
||||
|
||||
```python
|
||||
| @staticmethod
|
||||
| _get_select_related_if_apply(related: str, select_dict: Dict) -> Dict
|
||||
```
|
||||
|
||||
Extract nested part of select_related dictionary to extract models nested
|
||||
deeper on related model and already loaded in select related query.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `related (str)`: name of the relation
|
||||
- `select_dict (Dict)`: dictionary of select related models in main query
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Dict)`: dictionary with nested part of select related
|
||||
|
||||
<a name="queryset.prefetch_query.PrefetchQuery._update_already_loaded_rows"></a>
|
||||
#### \_update\_already\_loaded\_rows
|
||||
|
||||
```python
|
||||
| _update_already_loaded_rows(target_field: Type["BaseField"], prefetch_dict: Dict, orders_by: Dict) -> None
|
||||
```
|
||||
|
||||
Updates models that are already loaded, usually children of children.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `target_field (Type["BaseField"])`: ormar field with relation definition
|
||||
- `prefetch_dict (Dict)`: dictionaries of related models to prefetch
|
||||
- `orders_by (Dict)`: dictionary of order by clauses by model
|
||||
|
||||
<a name="queryset.prefetch_query.PrefetchQuery._populate_rows"></a>
|
||||
#### \_populate\_rows
|
||||
|
||||
```python
|
||||
| _populate_rows(rows: List, target_field: Type["BaseField"], parent_model: Type["Model"], table_prefix: str, fields: Union[Set[Any], Dict[Any, Any], None], exclude_fields: Union[Set[Any], Dict[Any, Any], None], prefetch_dict: Dict, orders_by: Dict) -> None
|
||||
```
|
||||
|
||||
Instantiates children models extracted from given relation.
|
||||
|
||||
Populates them with their own nested children if they are included in prefetch
|
||||
query.
|
||||
|
||||
Sets the initialized models and ids of them under corresponding keys in
|
||||
already_extracted dictionary. Later those instances will be fetched by ids
|
||||
and set on the parent model after sorting if needed.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `rows (List[sqlalchemy.engine.result.RowProxy])`: raw sql response from the prefetch query
|
||||
- `target_field (Type["BaseField"])`: field with relation definition from parent model
|
||||
- `parent_model (Type[Model])`: model with relation definition
|
||||
- `table_prefix (str)`: prefix of the target table from current relation
|
||||
- `fields (Union[Set[Any], Dict[Any, Any], None])`: fields to include
|
||||
- `exclude_fields (Union[Set[Any], Dict[Any, Any], None])`: fields to exclude
|
||||
- `prefetch_dict (Dict)`: dictionaries of related models to prefetch
|
||||
- `orders_by (Dict)`: dictionary of order by clauses by model
|
||||
|
||||
671
docs/api/query-set/query-set.md
Normal file
671
docs/api/query-set/query-set.md
Normal file
@ -0,0 +1,671 @@
|
||||
<a name="queryset.queryset"></a>
|
||||
# queryset.queryset
|
||||
|
||||
<a name="queryset.queryset.QuerySet"></a>
|
||||
## QuerySet Objects
|
||||
|
||||
```python
|
||||
class QuerySet()
|
||||
```
|
||||
|
||||
Main class to perform database queries, exposed on each model as objects attribute.
|
||||
|
||||
<a name="queryset.queryset.QuerySet.__init__"></a>
|
||||
#### \_\_init\_\_
|
||||
|
||||
```python
|
||||
| __init__(model_cls: Type["Model"] = None, filter_clauses: List = None, exclude_clauses: List = None, select_related: List = None, limit_count: int = None, offset: int = None, columns: Dict = None, exclude_columns: Dict = None, order_bys: List = None, prefetch_related: List = None, limit_raw_sql: bool = False) -> None
|
||||
```
|
||||
|
||||
<a name="queryset.queryset.QuerySet.__get__"></a>
|
||||
#### \_\_get\_\_
|
||||
|
||||
```python
|
||||
| __get__(instance: Optional[Union["QuerySet", "QuerysetProxy"]], owner: Union[Type["Model"], Type["QuerysetProxy"]]) -> "QuerySet"
|
||||
```
|
||||
|
||||
<a name="queryset.queryset.QuerySet.model_meta"></a>
|
||||
#### model\_meta
|
||||
|
||||
```python
|
||||
| @property
|
||||
| model_meta() -> "ModelMeta"
|
||||
```
|
||||
|
||||
Shortcut to model class Meta set on QuerySet model.
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(model Meta class)`: Meta class of the model
|
||||
|
||||
<a name="queryset.queryset.QuerySet.model"></a>
|
||||
#### model
|
||||
|
||||
```python
|
||||
| @property
|
||||
| model() -> Type["Model"]
|
||||
```
|
||||
|
||||
Shortcut to model class set on QuerySet.
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Type[Model])`: model class
|
||||
|
||||
<a name="queryset.queryset.QuerySet._prefetch_related_models"></a>
|
||||
#### \_prefetch\_related\_models
|
||||
|
||||
```python
|
||||
| async _prefetch_related_models(models: Sequence[Optional["Model"]], rows: List) -> Sequence[Optional["Model"]]
|
||||
```
|
||||
|
||||
Performs prefetch query for selected models names.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `models (List[Model])`: list of already parsed main Models from main query
|
||||
- `rows (List[sqlalchemy.engine.result.RowProxy])`: database rows from main query
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(List[Model])`: list of models with prefetch models populated
|
||||
|
||||
<a name="queryset.queryset.QuerySet._process_query_result_rows"></a>
|
||||
#### \_process\_query\_result\_rows
|
||||
|
||||
```python
|
||||
| _process_query_result_rows(rows: List) -> Sequence[Optional["Model"]]
|
||||
```
|
||||
|
||||
Process database rows and initialize ormar Model from each of the rows.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `rows (List[sqlalchemy.engine.result.RowProxy])`: list of database rows from query result
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(List[Model])`: list of models
|
||||
|
||||
<a name="queryset.queryset.QuerySet.check_single_result_rows_count"></a>
|
||||
#### check\_single\_result\_rows\_count
|
||||
|
||||
```python
|
||||
| @staticmethod
|
||||
| check_single_result_rows_count(rows: Sequence[Optional["Model"]]) -> None
|
||||
```
|
||||
|
||||
Verifies if the result has one and only one row.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `rows (List[Model])`: one element list of Models
|
||||
|
||||
<a name="queryset.queryset.QuerySet.database"></a>
|
||||
#### database
|
||||
|
||||
```python
|
||||
| @property
|
||||
| database() -> databases.Database
|
||||
```
|
||||
|
||||
Shortcut to models database from Meta class.
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(databases.Database)`: database
|
||||
|
||||
<a name="queryset.queryset.QuerySet.table"></a>
|
||||
#### table
|
||||
|
||||
```python
|
||||
| @property
|
||||
| table() -> sqlalchemy.Table
|
||||
```
|
||||
|
||||
Shortcut to models table from Meta class.
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(sqlalchemy.Table)`: database table
|
||||
|
||||
<a name="queryset.queryset.QuerySet.build_select_expression"></a>
|
||||
#### build\_select\_expression
|
||||
|
||||
```python
|
||||
| build_select_expression(limit: int = None, offset: int = None, order_bys: List = None) -> sqlalchemy.sql.select
|
||||
```
|
||||
|
||||
Constructs the actual database query used in the QuerySet.
|
||||
If any of the params is not passed the QuerySet own value is used.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `limit (int)`: number to limit the query
|
||||
- `offset (int)`: number to offset by
|
||||
- `order_bys (List)`: list of order-by fields names
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(sqlalchemy.sql.selectable.Select)`: built sqlalchemy select expression
|
||||
|
||||
<a name="queryset.queryset.QuerySet.filter"></a>
|
||||
#### filter
|
||||
|
||||
```python
|
||||
| filter(_exclude: bool = False, **kwargs: Any) -> "QuerySet"
|
||||
```
|
||||
|
||||
Allows you to filter by any `Model` attribute/field
|
||||
as well as to fetch instances, with a filter across an FK relationship.
|
||||
|
||||
You can use special filter suffix to change the filter operands:
|
||||
|
||||
* exact - like `album__name__exact='Malibu'` (exact match)
|
||||
* iexact - like `album__name__iexact='malibu'` (exact match case insensitive)
|
||||
* contains - like `album__name__contains='Mal'` (sql like)
|
||||
* icontains - like `album__name__icontains='mal'` (sql like case insensitive)
|
||||
* in - like `album__name__in=['Malibu', 'Barclay']` (sql in)
|
||||
* gt - like `position__gt=3` (sql >)
|
||||
* gte - like `position__gte=3` (sql >=)
|
||||
* lt - like `position__lt=3` (sql <)
|
||||
* lte - like `position__lte=3` (sql <=)
|
||||
* startswith - like `album__name__startswith='Mal'` (exact start match)
|
||||
* istartswith - like `album__name__istartswith='mal'` (case insensitive)
|
||||
* endswith - like `album__name__endswith='ibu'` (exact end match)
|
||||
* iendswith - like `album__name__iendswith='IBU'` (case insensitive)
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `_exclude (bool)`: flag if it should be exclude or filter
|
||||
- `kwargs (Any)`: fields names and proper value types
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(QuerySet)`: filtered QuerySet
|
||||
|
||||
<a name="queryset.queryset.QuerySet.exclude"></a>
|
||||
#### exclude
|
||||
|
||||
```python
|
||||
| exclude(**kwargs: Any) -> "QuerySet"
|
||||
```
|
||||
|
||||
Works exactly the same as filter and all modifiers (suffixes) are the same,
|
||||
but returns a *not* condition.
|
||||
|
||||
So if you use `filter(name='John')` which is `where name = 'John'` in SQL,
|
||||
the `exclude(name='John')` equals to `where name <> 'John'`
|
||||
|
||||
Note that all conditions are joined so if you pass multiple values it
|
||||
becomes a union of conditions.
|
||||
|
||||
`exclude(name='John', age>=35)` will become
|
||||
`where not (name='John' and age>=35)`
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `kwargs (Any)`: fields names and proper value types
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(QuerySet)`: filtered QuerySet
|
||||
|
||||
<a name="queryset.queryset.QuerySet.select_related"></a>
|
||||
#### select\_related
|
||||
|
||||
```python
|
||||
| select_related(related: Union[List, str]) -> "QuerySet"
|
||||
```
|
||||
|
||||
Allows to prefetch related models during the same query.
|
||||
|
||||
**With `select_related` always only one query is run against the database**,
|
||||
meaning that one (sometimes complicated) join is generated and later nested
|
||||
models are processed in python.
|
||||
|
||||
To fetch related model use `ForeignKey` names.
|
||||
|
||||
To chain related `Models` relation use double underscores between names.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `related (Union[List, str])`: list of relation field names, can be linked by '__' to nest
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(QuerySet)`: QuerySet
|
||||
|
||||
<a name="queryset.queryset.QuerySet.prefetch_related"></a>
|
||||
#### prefetch\_related
|
||||
|
||||
```python
|
||||
| prefetch_related(related: Union[List, str]) -> "QuerySet"
|
||||
```
|
||||
|
||||
Allows to prefetch related models during query - but opposite to
|
||||
`select_related` each subsequent model is fetched in a separate database query.
|
||||
|
||||
**With `prefetch_related` always one query per Model is run against the
|
||||
database**, meaning that you will have multiple queries executed one
|
||||
after another.
|
||||
|
||||
To fetch related model use `ForeignKey` names.
|
||||
|
||||
To chain related `Models` relation use double underscores between names.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `related (Union[List, str])`: list of relation field names, can be linked by '__' to nest
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(QuerySet)`: QuerySet
|
||||
|
||||
<a name="queryset.queryset.QuerySet.fields"></a>
|
||||
#### fields
|
||||
|
||||
```python
|
||||
| fields(columns: Union[List, str, Set, Dict]) -> "QuerySet"
|
||||
```
|
||||
|
||||
With `fields()` you can select subset of model columns to limit the data load.
|
||||
|
||||
Note that `fields()` and `exclude_fields()` works both for main models
|
||||
(on normal queries like `get`, `all` etc.)
|
||||
as well as `select_related` and `prefetch_related`
|
||||
models (with nested notation).
|
||||
|
||||
You can select specified fields by passing a `str, List[str], Set[str] or
|
||||
dict` with nested definition.
|
||||
|
||||
To include related models use notation
|
||||
`{related_name}__{column}[__{optional_next} etc.]`.
|
||||
|
||||
`fields()` can be called several times, building up the columns to select.
|
||||
|
||||
If you include related models into `select_related()` call but you won't specify
|
||||
columns for those models in fields - implies a list of all fields for
|
||||
those nested models.
|
||||
|
||||
Mandatory fields cannot be excluded as it will raise `ValidationError`,
|
||||
to exclude a field it has to be nullable.
|
||||
|
||||
Pk column cannot be excluded - it's always auto added even if
|
||||
not explicitly included.
|
||||
|
||||
You can also pass fields to include as dictionary or set.
|
||||
|
||||
To mark a field as included in a dictionary use it's name as key
|
||||
and ellipsis as value.
|
||||
|
||||
To traverse nested models use nested dictionaries.
|
||||
|
||||
To include fields at last level instead of nested dictionary a set can be used.
|
||||
|
||||
To include whole nested model specify model related field name and ellipsis.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `columns (Union[List, str, Set, Dict])`: columns to include
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(QuerySet)`: QuerySet
|
||||
|
||||
<a name="queryset.queryset.QuerySet.exclude_fields"></a>
|
||||
#### exclude\_fields
|
||||
|
||||
```python
|
||||
| exclude_fields(columns: Union[List, str, Set, Dict]) -> "QuerySet"
|
||||
```
|
||||
|
||||
With `exclude_fields()` you can select subset of model columns that will
|
||||
be excluded to limit the data load.
|
||||
|
||||
It's the opposite of `fields()` method so check documentation above
|
||||
to see what options are available.
|
||||
|
||||
Especially check above how you can pass also nested dictionaries
|
||||
and sets as a mask to exclude fields from whole hierarchy.
|
||||
|
||||
Note that `fields()` and `exclude_fields()` works both for main models
|
||||
(on normal queries like `get`, `all` etc.)
|
||||
as well as `select_related` and `prefetch_related` models
|
||||
(with nested notation).
|
||||
|
||||
Mandatory fields cannot be excluded as it will raise `ValidationError`,
|
||||
to exclude a field it has to be nullable.
|
||||
|
||||
Pk column cannot be excluded - it's always auto added even
|
||||
if explicitly excluded.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `columns (Union[List, str, Set, Dict])`: columns to exclude
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(QuerySet)`: QuerySet
|
||||
|
||||
<a name="queryset.queryset.QuerySet.order_by"></a>
|
||||
#### order\_by
|
||||
|
||||
```python
|
||||
| order_by(columns: Union[List, str]) -> "QuerySet"
|
||||
```
|
||||
|
||||
With `order_by()` you can order the results from database based on your
|
||||
choice of fields.
|
||||
|
||||
You can provide a string with field name or list of strings with fields names.
|
||||
|
||||
Ordering in sql will be applied in order of names you provide in order_by.
|
||||
|
||||
By default if you do not provide ordering `ormar` explicitly orders by
|
||||
all primary keys
|
||||
|
||||
If you are sorting by nested models that causes that the result rows are
|
||||
unsorted by the main model `ormar` will combine those children rows into
|
||||
one main model.
|
||||
|
||||
The main model will never duplicate in the result
|
||||
|
||||
To order by main model field just provide a field name
|
||||
|
||||
To sort on nested models separate field names with dunder '__'.
|
||||
|
||||
You can sort this way across all relation types -> `ForeignKey`,
|
||||
reverse virtual FK and `ManyToMany` fields.
|
||||
|
||||
To sort in descending order provide a hyphen in front of the field name
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `columns (Union[List, str])`: columns by which models should be sorted
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(QuerySet)`: QuerySet
|
||||
|
||||
<a name="queryset.queryset.QuerySet.exists"></a>
|
||||
#### exists
|
||||
|
||||
```python
|
||||
| async exists() -> bool
|
||||
```
|
||||
|
||||
Returns a bool value to confirm if there are rows matching the given criteria
|
||||
(applied with `filter` and `exclude` if set).
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(bool)`: result of the check
|
||||
|
||||
<a name="queryset.queryset.QuerySet.count"></a>
|
||||
#### count
|
||||
|
||||
```python
|
||||
| async count() -> int
|
||||
```
|
||||
|
||||
Returns number of rows matching the given criteria
|
||||
(applied with `filter` and `exclude` if set before).
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(int)`: number of rows
|
||||
|
||||
<a name="queryset.queryset.QuerySet.update"></a>
|
||||
#### update
|
||||
|
||||
```python
|
||||
| async update(each: bool = False, **kwargs: Any) -> int
|
||||
```
|
||||
|
||||
Updates the model table after applying the filters from kwargs.
|
||||
|
||||
You have to either pass a filter to narrow down a query or explicitly pass
|
||||
each=True flag to affect whole table.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `each (bool)`: flag if whole table should be affected if no filter is passed
|
||||
- `kwargs (Any)`: fields names and proper value types
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(int)`: number of updated rows
|
||||
|
||||
<a name="queryset.queryset.QuerySet.delete"></a>
|
||||
#### delete
|
||||
|
||||
```python
|
||||
| async delete(each: bool = False, **kwargs: Any) -> int
|
||||
```
|
||||
|
||||
Deletes from the model table after applying the filters from kwargs.
|
||||
|
||||
You have to either pass a filter to narrow down a query or explicitly pass
|
||||
each=True flag to affect whole table.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `each (bool)`: flag if whole table should be affected if no filter is passed
|
||||
- `kwargs (Any)`: fields names and proper value types
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(int)`: number of deleted rows
|
||||
|
||||
<a name="queryset.queryset.QuerySet.limit"></a>
|
||||
#### limit
|
||||
|
||||
```python
|
||||
| limit(limit_count: int, limit_raw_sql: bool = None) -> "QuerySet"
|
||||
```
|
||||
|
||||
You can limit the results to desired number of parent models.
|
||||
|
||||
To limit the actual number of database query rows instead of number of main
|
||||
models use the `limit_raw_sql` parameter flag, and set it to `True`.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `limit_raw_sql (bool)`: flag if raw sql should be limited
|
||||
- `limit_count (int)`: number of models to limit
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(QuerySet)`: QuerySet
|
||||
|
||||
<a name="queryset.queryset.QuerySet.offset"></a>
|
||||
#### offset
|
||||
|
||||
```python
|
||||
| offset(offset: int, limit_raw_sql: bool = None) -> "QuerySet"
|
||||
```
|
||||
|
||||
You can also offset the results by desired number of main models.
|
||||
|
||||
To offset the actual number of database query rows instead of number of main
|
||||
models use the `limit_raw_sql` parameter flag, and set it to `True`.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `limit_raw_sql (bool)`: flag if raw sql should be offset
|
||||
- `offset (int)`: numbers of models to offset
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(QuerySet)`: QuerySet
|
||||
|
||||
<a name="queryset.queryset.QuerySet.first"></a>
|
||||
#### first
|
||||
|
||||
```python
|
||||
| async first(**kwargs: Any) -> "Model"
|
||||
```
|
||||
|
||||
Gets the first row from the db ordered by primary key column ascending.
|
||||
|
||||
**Raises**:
|
||||
|
||||
- `NoMatch`: if no rows are returned
|
||||
- `MultipleMatches`: if more than 1 row is returned.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `kwargs (Any)`: fields names and proper value types
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Model)`: returned model
|
||||
|
||||
<a name="queryset.queryset.QuerySet.get"></a>
|
||||
#### get
|
||||
|
||||
```python
|
||||
| async get(**kwargs: Any) -> "Model"
|
||||
```
|
||||
|
||||
Get's the first row from the db meeting the criteria set by kwargs.
|
||||
|
||||
If no criteria set it will return the last row in db sorted by pk.
|
||||
|
||||
Passing a criteria is actually calling filter(**kwargs) method described below.
|
||||
|
||||
**Raises**:
|
||||
|
||||
- `NoMatch`: if no rows are returned
|
||||
- `MultipleMatches`: if more than 1 row is returned.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `kwargs (Any)`: fields names and proper value types
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Model)`: returned model
|
||||
|
||||
<a name="queryset.queryset.QuerySet.get_or_create"></a>
|
||||
#### get\_or\_create
|
||||
|
||||
```python
|
||||
| async get_or_create(**kwargs: Any) -> "Model"
|
||||
```
|
||||
|
||||
Combination of create and get methods.
|
||||
|
||||
Tries to get a row meeting the criteria fro kwargs
|
||||
and if `NoMatch` exception is raised
|
||||
it creates a new one with given kwargs.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `kwargs (Any)`: fields names and proper value types
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Model)`: returned or created Model
|
||||
|
||||
<a name="queryset.queryset.QuerySet.update_or_create"></a>
|
||||
#### update\_or\_create
|
||||
|
||||
```python
|
||||
| async update_or_create(**kwargs: Any) -> "Model"
|
||||
```
|
||||
|
||||
Updates the model, or in case there is no match in database creates a new one.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `kwargs (Any)`: fields names and proper value types
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Model)`: updated or created model
|
||||
|
||||
<a name="queryset.queryset.QuerySet.all"></a>
|
||||
#### all
|
||||
|
||||
```python
|
||||
| async all(**kwargs: Any) -> Sequence[Optional["Model"]]
|
||||
```
|
||||
|
||||
Returns all rows from a database for given model for set filter options.
|
||||
|
||||
Passing kwargs is a shortcut and equals to calling `filter(**kwrags).all()`.
|
||||
|
||||
If there are no rows meeting the criteria an empty list is returned.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `kwargs (Any)`: fields names and proper value types
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(List[Model])`: list of returned models
|
||||
|
||||
<a name="queryset.queryset.QuerySet.create"></a>
|
||||
#### create
|
||||
|
||||
```python
|
||||
| async create(**kwargs: Any) -> "Model"
|
||||
```
|
||||
|
||||
Creates the model instance, saves it in a database and returns the updates model
|
||||
(with pk populated if not passed and autoincrement is set).
|
||||
|
||||
The allowed kwargs are `Model` fields names and proper value types.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `kwargs (Any)`: fields names and proper value types
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(Model)`: created model
|
||||
|
||||
<a name="queryset.queryset.QuerySet.bulk_create"></a>
|
||||
#### bulk\_create
|
||||
|
||||
```python
|
||||
| async bulk_create(objects: List["Model"]) -> None
|
||||
```
|
||||
|
||||
Performs a bulk update in one database session to speed up the process.
|
||||
|
||||
Allows you to create multiple objects at once.
|
||||
|
||||
A valid list of `Model` objects needs to be passed.
|
||||
|
||||
Bulk operations do not send signals.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `objects (List[Model])`: list of ormar models already initialized and ready to save.
|
||||
|
||||
<a name="queryset.queryset.QuerySet.bulk_update"></a>
|
||||
#### bulk\_update
|
||||
|
||||
```python
|
||||
| async bulk_update(objects: List["Model"], columns: List[str] = None) -> None
|
||||
```
|
||||
|
||||
Performs bulk update in one database session to speed up the process.
|
||||
|
||||
Allows to update multiple instance at once.
|
||||
|
||||
All `Models` passed need to have primary key column populated.
|
||||
|
||||
You can also select which fields to update by passing `columns` list
|
||||
as a list of string names.
|
||||
|
||||
Bulk operations do not send signals.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `objects (List[Model])`: list of ormar models
|
||||
- `columns (List[str])`: list of columns to update
|
||||
|
||||
157
docs/api/query-set/query.md
Normal file
157
docs/api/query-set/query.md
Normal file
@ -0,0 +1,157 @@
|
||||
<a name="queryset.query"></a>
|
||||
# queryset.query
|
||||
|
||||
<a name="queryset.query.Query"></a>
|
||||
## Query Objects
|
||||
|
||||
```python
|
||||
class Query()
|
||||
```
|
||||
|
||||
<a name="queryset.query.Query.__init__"></a>
|
||||
#### \_\_init\_\_
|
||||
|
||||
```python
|
||||
| __init__(model_cls: Type["Model"], filter_clauses: List, exclude_clauses: List, select_related: List, limit_count: Optional[int], offset: Optional[int], fields: Optional[Union[Dict, Set]], exclude_fields: Optional[Union[Dict, Set]], order_bys: Optional[List], limit_raw_sql: bool) -> None
|
||||
```
|
||||
|
||||
<a name="queryset.query.Query._init_sorted_orders"></a>
|
||||
#### \_init\_sorted\_orders
|
||||
|
||||
```python
|
||||
| _init_sorted_orders() -> None
|
||||
```
|
||||
|
||||
Initialize empty order_by dict to be populated later during the query call
|
||||
|
||||
<a name="queryset.query.Query.prefixed_pk_name"></a>
|
||||
#### prefixed\_pk\_name
|
||||
|
||||
```python
|
||||
| @property
|
||||
| prefixed_pk_name() -> str
|
||||
```
|
||||
|
||||
Shortcut for extracting prefixed with alias primary key column name from main
|
||||
model
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(str)`: alias of pk column prefix with table name.
|
||||
|
||||
<a name="queryset.query.Query.alias"></a>
|
||||
#### alias
|
||||
|
||||
```python
|
||||
| alias(name: str) -> str
|
||||
```
|
||||
|
||||
Shortcut to extracting column alias from given master model.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `name (str)`: name of column
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(str)`: alias of given column name
|
||||
|
||||
<a name="queryset.query.Query.apply_order_bys_for_primary_model"></a>
|
||||
#### apply\_order\_bys\_for\_primary\_model
|
||||
|
||||
```python
|
||||
| apply_order_bys_for_primary_model() -> None
|
||||
```
|
||||
|
||||
Applies order_by queries on main model when it's used as a subquery.
|
||||
That way the subquery with limit and offset only on main model has proper
|
||||
sorting applied and correct models are fetched.
|
||||
|
||||
<a name="queryset.query.Query._pagination_query_required"></a>
|
||||
#### \_pagination\_query\_required
|
||||
|
||||
```python
|
||||
| _pagination_query_required() -> bool
|
||||
```
|
||||
|
||||
Checks if limit or offset are set, the flag limit_sql_raw is not set
|
||||
and query has select_related applied. Otherwise we can limit/offset normally
|
||||
at the end of whole query.
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(bool)`: result of the check
|
||||
|
||||
<a name="queryset.query.Query.build_select_expression"></a>
|
||||
#### build\_select\_expression
|
||||
|
||||
```python
|
||||
| build_select_expression() -> Tuple[sqlalchemy.sql.select, List[str]]
|
||||
```
|
||||
|
||||
Main entry point from outside (after proper initialization).
|
||||
|
||||
Extracts columns list to fetch,
|
||||
construct all required joins for select related,
|
||||
then applies all conditional and sort clauses.
|
||||
|
||||
Returns ready to run query with all joins and clauses.
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(sqlalchemy.sql.selectable.Select)`: ready to run query with all joins and clauses.
|
||||
|
||||
<a name="queryset.query.Query._build_pagination_subquery"></a>
|
||||
#### \_build\_pagination\_subquery
|
||||
|
||||
```python
|
||||
| _build_pagination_subquery() -> sqlalchemy.sql.select
|
||||
```
|
||||
|
||||
In order to apply limit and offset on main table in join only
|
||||
(otherwise you can get only partially constructed main model
|
||||
if number of children exceeds the applied limit and select_related is used)
|
||||
|
||||
Used also to change first and get() without argument behaviour.
|
||||
Needed only if limit or offset are set, the flag limit_sql_raw is not set
|
||||
and query has select_related applied. Otherwise we can limit/offset normally
|
||||
at the end of whole query.
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(sqlalchemy.sql.select)`: constructed subquery on main table with limit, offset and order applied
|
||||
|
||||
<a name="queryset.query.Query._apply_expression_modifiers"></a>
|
||||
#### \_apply\_expression\_modifiers
|
||||
|
||||
```python
|
||||
| _apply_expression_modifiers(expr: sqlalchemy.sql.select) -> sqlalchemy.sql.select
|
||||
```
|
||||
|
||||
Receives the select query (might be join) and applies:
|
||||
* Filter clauses
|
||||
* Exclude filter clauses
|
||||
* Limit clauses
|
||||
* Offset clauses
|
||||
* Order by clauses
|
||||
|
||||
Returns complete ready to run query.
|
||||
|
||||
**Arguments**:
|
||||
|
||||
- `expr (sqlalchemy.sql.selectable.Select)`: select expression before clauses
|
||||
|
||||
**Returns**:
|
||||
|
||||
`(sqlalchemy.sql.selectable.Select)`: expresion with all present clauses applied
|
||||
|
||||
<a name="queryset.query.Query._reset_query_parameters"></a>
|
||||
#### \_reset\_query\_parameters
|
||||
|
||||
```python
|
||||
| _reset_query_parameters() -> None
|
||||
```
|
||||
|
||||
Although it should be created each time before the call we reset the key params
|
||||
anyway.
|
||||
|
||||
152
docs/api/query-set/utils.md
Normal file
152
docs/api/query-set/utils.md
Normal file
@ -0,0 +1,152 @@
|
||||
<a name="queryset.utils"></a>
|
||||
# queryset.utils
|
||||
|
||||
<a name="queryset.utils.check_node_not_dict_or_not_last_node"></a>
|
||||
#### check\_node\_not\_dict\_or\_not\_last\_node
|
||||
|
||||
```python
|
||||
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
|
||||
|
||||
<a name="queryset.utils.translate_list_to_dict"></a>
|
||||
#### translate\_list\_to\_dict
|
||||
|
||||
```python
|
||||
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
|
||||
|
||||
<a name="queryset.utils.convert_set_to_required_dict"></a>
|
||||
#### convert\_set\_to\_required\_dict
|
||||
|
||||
```python
|
||||
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
|
||||
|
||||
<a name="queryset.utils.update"></a>
|
||||
#### update
|
||||
|
||||
```python
|
||||
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
|
||||
|
||||
<a name="queryset.utils.update_dict_from_list"></a>
|
||||
#### update\_dict\_from\_list
|
||||
|
||||
```python
|
||||
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
|
||||
|
||||
<a name="queryset.utils.extract_nested_models"></a>
|
||||
#### extract\_nested\_models
|
||||
|
||||
```python
|
||||
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
|
||||
|
||||
<a name="queryset.utils.extract_models_to_dict_of_lists"></a>
|
||||
#### extract\_models\_to\_dict\_of\_lists
|
||||
|
||||
```python
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user