update docs, add load_all(), tests for load_all, make through field optional

This commit is contained in:
collerek
2021-03-03 19:48:40 +01:00
parent 9ad1528cc0
commit a8ae50276e
56 changed files with 1653 additions and 653 deletions

View File

@ -120,7 +120,7 @@ Adds alias to the dictionary of aliases under given key.
#### resolve\_relation\_alias
```python
| resolve_relation_alias(from_model: Type["Model"], relation_name: str) -> str
| resolve_relation_alias(from_model: Union[Type["Model"], Type["ModelRow"]], relation_name: str) -> str
```
Given model and relation name returns the alias for this relation.
@ -134,3 +134,24 @@ Given model and relation name returns the alias for this relation.
`(str)`: alias of the relation
<a name="relations.alias_manager.AliasManager.resolve_relation_alias_after_complex"></a>
#### resolve\_relation\_alias\_after\_complex
```python
| resolve_relation_alias_after_complex(source_model: Union[Type["Model"], Type["ModelRow"]], relation_str: str, relation_field: Type["ForeignKeyField"]) -> str
```
Given source model and relation string returns the alias for this complex
relation if it exists, otherwise fallback to normal relation from a relation
field definition.
**Arguments**:
- `relation_field (Type["ForeignKeyField"])`: field with direct relation definition
- `source_model (source Model)`: model with query starts
- `relation_str (str)`: string with relation joins defined
**Returns**:
`(str)`: alias of the relation

View File

@ -5,7 +5,7 @@
## QuerysetProxy Objects
```python
class QuerysetProxy(ormar.QuerySetProtocol)
class QuerysetProxy()
```
Exposes QuerySet methods on relations, but also handles creating and removing
@ -43,7 +43,7 @@ Set's the queryset. Initialized in RelationProxy.
#### \_assign\_child\_to\_parent
```python
| _assign_child_to_parent(child: Optional["T"]) -> None
| _assign_child_to_parent(child: Optional["Model"]) -> None
```
Registers child in parents RelationManager.
@ -56,7 +56,7 @@ Registers child in parents RelationManager.
#### \_register\_related
```python
| _register_related(child: Union["T", Sequence[Optional["T"]]]) -> None
| _register_related(child: Union["Model", Sequence[Optional["Model"]]]) -> None
```
Registers child/ children in parents RelationManager.
@ -78,20 +78,35 @@ Cleans the current list of the related models.
#### create\_through\_instance
```python
| async create_through_instance(child: "T") -> None
| async create_through_instance(child: "Model", **kwargs: Any) -> None
```
Crete a through model instance in the database for m2m relations.
**Arguments**:
- `kwargs (Any)`: dict of additional keyword arguments for through instance
- `child (Model)`: child model instance
<a name="relations.querysetproxy.QuerysetProxy.update_through_instance"></a>
#### update\_through\_instance
```python
| async update_through_instance(child: "Model", **kwargs: Any) -> None
```
Updates a through model instance in the database for m2m relations.
**Arguments**:
- `kwargs (Any)`: dict of additional keyword arguments for through instance
- `child (Model)`: child model instance
<a name="relations.querysetproxy.QuerysetProxy.delete_through_instance"></a>
#### delete\_through\_instance
```python
| async delete_through_instance(child: "T") -> None
| async delete_through_instance(child: "Model") -> None
```
Removes through model instance from the database for m2m relations.
@ -256,6 +271,27 @@ Actual call delegated to QuerySet.
`(Model)`: created model
<a name="relations.querysetproxy.QuerysetProxy.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="relations.querysetproxy.QuerysetProxy.get_or_create"></a>
#### get\_or\_create

View File

@ -10,37 +10,6 @@ class RelationsManager()
Manages relations on a Model, each Model has it's own instance.
<a name="relations.relation_manager.RelationsManager._get_relation_type"></a>
#### \_get\_relation\_type
```python
| _get_relation_type(field: Type[BaseField]) -> RelationType
```
Returns type of the relation declared on a field.
**Arguments**:
- `field (Type[BaseField])`: field with relation declaration
**Returns**:
`(RelationType)`: type of the relation defined on field
<a name="relations.relation_manager.RelationsManager._add_relation"></a>
#### \_add\_relation
```python
| _add_relation(field: Type[BaseField]) -> None
```
Registers relation in the manager.
Adds Relation instance under field.name.
**Arguments**:
- `field (Type[BaseField])`: field with relation declaration
<a name="relations.relation_manager.RelationsManager.__contains__"></a>
#### \_\_contains\_\_
@ -62,7 +31,7 @@ Checks if relation with given name is already registered.
#### get
```python
| get(name: str) -> Optional[Union["T", Sequence["T"]]]
| get(name: str) -> Optional[Union["Model", Sequence["Model"]]]
```
Returns the related model/models if relation is set.
@ -76,23 +45,6 @@ Actual call is delegated to Relation instance registered under relation name.
`(Optional[Union[Model, List[Model]])`: related model or list of related models if set
<a name="relations.relation_manager.RelationsManager._get"></a>
#### \_get
```python
| _get(name: str) -> Optional[Relation]
```
Returns the actual relation and not the related model(s).
**Arguments**:
- `name (str)`: name of the relation
**Returns**:
`(ormar.relations.relation.Relation)`: Relation instance
<a name="relations.relation_manager.RelationsManager.add"></a>
#### add
@ -148,3 +100,51 @@ of relation from which you want to remove the parent.
- `parent (Model)`: parent Model
- `name (str)`: name of the relation
<a name="relations.relation_manager.RelationsManager._get"></a>
#### \_get
```python
| _get(name: str) -> Optional[Relation]
```
Returns the actual relation and not the related model(s).
**Arguments**:
- `name (str)`: name of the relation
**Returns**:
`(ormar.relations.relation.Relation)`: Relation instance
<a name="relations.relation_manager.RelationsManager._get_relation_type"></a>
#### \_get\_relation\_type
```python
| _get_relation_type(field: Type["BaseField"]) -> RelationType
```
Returns type of the relation declared on a field.
**Arguments**:
- `field (Type[BaseField])`: field with relation declaration
**Returns**:
`(RelationType)`: type of the relation defined on field
<a name="relations.relation_manager.RelationsManager._add_relation"></a>
#### \_add\_relation
```python
| _add_relation(field: Type["BaseField"]) -> None
```
Registers relation in the manager.
Adds Relation instance under field.name.
**Arguments**:
- `field (Type[BaseField])`: field with relation declaration

View File

@ -131,7 +131,7 @@ will be deleted, and not only removed from relation).
#### add
```python
| async add(item: "Model") -> None
| async add(item: "Model", **kwargs: Any) -> None
```
Adds child model to relation.
@ -140,5 +140,6 @@ For ManyToMany relations through instance is automatically created.
**Arguments**:
- `kwargs (Any)`: dict of additional keyword arguments for through instance
- `item (Model)`: child to add to relation

View File

@ -27,7 +27,7 @@ Keeps related Models and handles adding/removing of the children.
#### \_\_init\_\_
```python
| __init__(manager: "RelationsManager", type_: RelationType, field_name: str, to: Type["T"], through: Type["T"] = None) -> None
| __init__(manager: "RelationsManager", type_: RelationType, field_name: str, to: Type["Model"], through: Type["Model"] = None) -> None
```
Initialize the Relation and keep the related models either as instances of
@ -73,7 +73,7 @@ Find child model in RelationProxy if exists.
#### add
```python
| add(child: "T") -> None
| add(child: "Model") -> None
```
Adds child Model to relation, either sets child as related model or adds
@ -101,7 +101,7 @@ it from the list in RelationProxy depending on relation type.
#### get
```python
| get() -> Optional[Union[List["T"], "T"]]
| get() -> Optional[Union[List["Model"], "Model"]]
```
Return the related model or models from RelationProxy.