update docs part 1

This commit is contained in:
collerek
2020-10-06 19:09:58 +02:00
parent dd46dbcfd4
commit ba0990d05b
11 changed files with 505 additions and 212 deletions

View File

@ -21,7 +21,7 @@ Each table **has to** have a primary key column, which you specify by setting `p
Only one primary key column is allowed.
```Python hl_lines="14 15 16"
```Python hl_lines="15 16 17"
--8<-- "../docs_src/models/docs001.py"
```
@ -34,7 +34,7 @@ By default if you assign primary key to `Integer` field, the `autoincrement` opt
You can disable by passing `autoincremant=False`.
```Python
id = ormar.Integer(primary_key=True, autoincrement=False)
id: ormar.Integer(primary_key=True, autoincrement=False)
```
Names of the fields will be used for both the underlying `pydantic` model and `sqlalchemy` table.
@ -48,9 +48,9 @@ and table creation you need to assign each `Model` with two special parameters.
One is `Database` instance created with your database url in [sqlalchemy connection string][sqlalchemy connection string] format.
Created instance needs to be passed to every `Model` with `__database__` parameter.
Created instance needs to be passed to every `Model` with `Meta` class `database` parameter.
```Python hl_lines="1 6 11"
```Python hl_lines="1 6 12"
--8<-- "../docs_src/models/docs001.py"
```
@ -62,9 +62,9 @@ Created instance needs to be passed to every `Model` with `__database__` paramet
Second dependency is sqlalchemy `MetaData` instance.
Created instance needs to be passed to every `Model` with `__metadata__` parameter.
Created instance needs to be passed to every `Model` with `Meta` class `metadata` parameter.
```Python hl_lines="2 7 12"
```Python hl_lines="2 7 13"
--8<-- "../docs_src/models/docs001.py"
```
@ -76,9 +76,9 @@ Created instance needs to be passed to every `Model` with `__metadata__` paramet
By default table name is created from Model class name as lowercase name plus 's'.
You can overwrite this parameter by providing `__tablename__` argument.
You can overwrite this parameter by providing `Meta` class `tablename` argument.
```Python hl_lines="11 12 13"
```Python hl_lines="12 13 14"
--8<-- "../docs_src/models/docs002.py"
```
@ -91,7 +91,7 @@ There are two ways to create and persist the `Model` instance in the database.
If you plan to modify the instance in the later execution of your program you can initiate your `Model` as a normal class and later await a `save()` call.
```Python hl_lines="19 20"
```Python hl_lines="20 21"
--8<-- "../docs_src/models/docs007.py"
```
@ -99,41 +99,24 @@ If you want to initiate your `Model` and at the same time save in in the databas
Each model has a `QuerySet` initialised as `objects` parameter
```Python hl_lines="22"
```Python hl_lines="23"
--8<-- "../docs_src/models/docs007.py"
```
!!!info
To read more about `QuerySets` and available methods visit [queries][queries]
## Attributes Delegation
Each call to `Model` fields parameter under the hood is delegated to either the `pydantic` model
or other related `Model` in case of relations.
The fields and relations are not stored on the `Model` itself
```Python hl_lines="31 32 33 34 35 36 37 38 39 40 41"
--8<-- "../docs_src/models/docs006.py"
```
!!! warning
In example above model instances are created but not persisted that's why `id` of `department` is None!
!!!info
To read more about `ForeignKeys` and `Model` relations visit [relations][relations]
## Internals
Apart from special parameters defined in the `Model` during definition (tablename, metadata etc.) the `Model` provides you with useful internals.
### Pydantic Model
To access auto created pydantic model you can use `Model.__pydantic_model__` parameter
All `Model` classes inherit from `pydantic.BaseModel` so you can access all normal attributes of pydantic models.
For example to list model fields you can:
```Python hl_lines="18"
```Python hl_lines="20"
--8<-- "../docs_src/models/docs003.py"
```
@ -145,11 +128,11 @@ For example to list model fields you can:
### Sqlalchemy Table
To access auto created sqlalchemy table you can use `Model.__table__` parameter
To access auto created sqlalchemy table you can use `Model.Meta.table` parameter
For example to list table columns you can:
```Python hl_lines="18"
```Python hl_lines="20"
--8<-- "../docs_src/models/docs004.py"
```
@ -161,14 +144,26 @@ For example to list table columns you can:
### Fields Definition
To access ormar `Fields` you can use `Model.__model_fields__` parameter
To access ormar `Fields` you can use `Model.Meta.model_fields` parameter
For example to list table model fields you can:
```Python hl_lines="18"
```Python hl_lines="19"
--8<-- "../docs_src/models/docs005.py"
```
!!!info
Note that fields stored on a model are `classes` not `instances`.
So if you print just model fields you will get:
`{'id': <class 'ormar.fields.model_fields.Integer'>, `
`'name': <class 'ormar.fields.model_fields.String'>, `
`'completed': <class 'ormar.fields.model_fields.Boolean'>}`
[fields]: ./fields.md
[relations]: ./relations.md
[queries]: ./queries.md