8000 proofread and fix the docs for models · collerek/ormar@6b0245d · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 6b0245d

Browse files
committed
proofread and fix the docs for models
1 parent 280acc1 commit 6b0245d

File tree

2 files changed

+50
-52
lines changed

2 files changed

+50
-52
lines changed

docs/models/inheritance.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Out of various types of ORM models inheritance `ormar` currently supports two of
77

88
## Types of inheritance
99

10-
The short summary of different types of inheritance is:
10+
The short summary of different types of inheritance:
1111

1212
* **Mixins [SUPPORTED]** - don't subclass `ormar.Model`, just define fields that are
1313
later used on different models (like `created_date` and `updated_date` on each model),
@@ -126,8 +126,6 @@ class Category(DateFieldsModel, AuditModel):
126126
The list of inherited options/settings is as follows: `metadata`, `database`
127127
and `constraints`.
128128

129-
Also methods decorated with `@property_field` decorator will be inherited/recognized.
130-
131129
Of course apart from that all fields from base classes are combined and created in the
132130
concrete table of the final Model.
133131

@@ -143,7 +141,7 @@ inheritance.
143141
Whenever you define a field with same name and new definition it will completely replace
144142
the previously defined one.
145143

146-
```python
144+
```python hl_lines="28"
147145
# base class
148146
class DateFieldsModel(ormar.Model):
149147
ormar_config = OrmarConfig(
@@ -324,7 +322,7 @@ Person.ormar_config.model_fields
324322
Similarly, you can inherit from Models that have ManyToMany relations declared but
325323
there is one, but substantial difference - the Through model.
326324

327-
Since in the future the Through model will be able to hold additional fields and now it links only two Tables
325+
Since the Through model will be able to hold additional fields, and now it links only two Tables
328326
(`from` and `to` ones), each child that inherits the m2m relation field has to have separate
329327
Through model.
330328

docs/models/methods.md

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,37 @@ Available methods are described below.
1313
## `pydantic` methods
1414

1515
Note that each `ormar.Model` is also a `pydantic.BaseModel`, so all `pydantic` methods are also available on a model,
16-
especially `model_dump()` and `json()` methods that can also accept `exclude`, `include` and other parameters.
16+
especially `model_dump()` and `model_dump_json()` methods that can also accept `exclude`, `include` and other parameters.
1717

1818
To read more check [pydantic][pydantic] documentation
1919

20-
## construct
20+
## model_construct()
2121

22-
`construct` is a raw equivalent of `__init__` method used for construction of new instances.
22+
`model_construct` is a raw equivalent of `__init__` method used for construction of new instances.
2323

24-
The difference is that `construct` skips validations, so it should be used when you know that data is correct and can be trusted.
24+
The difference is that `model_construct` skips validations, so it should be used when you know that data is correct and can be trusted.
2525
The benefit of using construct is the speed of execution due to skipped validation.
2626

2727
!!!note
28-
Note that in contrast to `pydantic.construct` method - the `ormar` equivalent will also process the nested related models.
28+
Note that in contrast to `pydantic.model_construct` method - the `ormar` equivalent will also process the nested related models.
2929

3030
!!!warning
31-
Bear in mind that due to skipped validation the `construct` method does not perform any conversions, checks etc.
32-
So it's your responsibility to provide that data that is valid and can be consumed by the database.
33-
34-
The only two things that construct still performs are:
31+
Bear in mind that due to skipped validation the `construct` method does not perform any conversions, checks etc.
32+
So it's your responsibility to provide that data that is valid and can be consumed by the database.
33+
34+
The only two things that construct still performs are:
3535

36-
* Providing a `default` value for not set fields
37-
* Initialize nested ormar models if you pass a dictionary or a primary key value
36+
* Providing a `default` value for not set fields
37+
* Initialize nested ormar models if you pass a dictionary or a primary key value
3838

39-
## dict
39+
## model_dump()
4040

41-
`dict` is a method inherited from `pydantic`, yet `ormar` adds its own parameters and has some nuances when working with default values,
41+
`model_dump` is a method inherited from `pydantic`, yet `ormar` adds its own parameters and has some nuances when working with default values,
4242
therefore it's listed here for clarity.
4343

44-
`dict` as the name suggests export data from model tree to dictionary.
44+
`model_dump` as the name suggests export data from model tree to dictionary.
4545

46-
Explanation of dict parameters:
46+
Explanation of model_dump parameters:
4747

4848
### include (`ormar` modified)
4949

@@ -55,14 +55,14 @@ Note that `pydantic` has an uncommon pattern of including/ excluding fields in l
5555
And if you want to exclude the field in all children you need to pass a `__all__` key to dictionary.
5656

5757
You cannot exclude nested models in `Set`s in `pydantic` but you can in `ormar`
58-
(by adding double underscore on relation name i.e. to exclude name of category for a book you cen use `exclude={"book__category__name"}`)
58+
(by adding double underscore on relation name i.e. to exclude name of category for a book you can use `exclude={"book__category__name"}`)
5959

6060
`ormar` does not support by index exclusion/ inclusions and accepts a simplified and more user-friendly notation.
6161

6262
To check how you can include/exclude fields, including nested fields check out [fields](../queries/select-columns.md#fields) section that has an explanation and a lot of samples.
6363

6464
!!!note
65-
The fact that in `ormar` you can exclude nested models in sets, you can exclude from a whole model tree in `response_model_exclude` and `response_model_include` in fastapi!
65+
The fact that in `ormar` you can exclude nested models in sets, you can exclude from a whole model tree in `response_model_exclude` and `response_model_include` in fastapi!
6666

6767
### exclude (`ormar` modified)
6868

@@ -81,7 +81,7 @@ You cannot exclude nested models in `Set`s in `pydantic` but you can in `ormar`
8181
To check how you can include/exclude fields, including nested fields check out [fields](../queries/select-columns.md#fields) section that has an explanation and a lot of samples.
8282

8383
!!!note
84-
The fact that in `ormar` you can exclude nested models in sets, you can exclude from a whole model tree in `response_model_exclude` and `response_model_include` in fastapi!
84+
The fact that in `ormar` you can exclude nested models in sets, you can exclude from a whole model tree in `response_model_exclude` and `response_model_include` in fastapi!
8585

8686
### exclude_unset
8787

@@ -90,9 +90,9 @@ To check how you can include/exclude fields, including nested fields check out [
9090
Flag indicates whether fields which were not explicitly set when creating the model should be excluded from the returned dictionary.
9191

9292
!!!warning
93-
Note that after you save data into database each field has its own value -> either provided by you, default, or `None`.
94-
95-
That means that when you load the data from database, **all** fields are set, and this flag basically stop working!
93+
Note that after you save data into database each field has its own value -> either provided by you, default, or `None`.
94+
95+
That means that when you load the data from database, **all** fields are set, and this flag basically stop working!
9696

9797
```python
9898
class Category(ormar.Model):
@@ -270,26 +270,26 @@ assert item.model_dump(exclude_through_models=True) == {
270270
]}
271271
```
272272

273-
## json
273+
## model_dump_json()
274274

275-
`json()` has exactly the same parameters as `model_dump()` so check above.
275+
`model_dump_json()` has exactly the same parameters as `model_dump()` so check above.
276276

277277
Of course the end result is a string with json representation and not a dictionary.
278278

279-
## get_pydantic
279+
## get_pydantic()
280280

281281
`get_pydantic(include: Union[Set, Dict] = None, exclude: Union[Set, Dict] = None)`
282282

283283
This method allows you to generate `pydantic` models from your ormar models without you needing to retype all the fields.
284284

285-
Note that if you have nested models, it **will generate whole tree of pydantic models for you!**
285+
Note that if you have nested models, it **will generate whole tree of pydantic models for you!** but in a way that prevents cyclic references issues.
286286

287287
Moreover, you can pass `exclude` and/or `include` parameters to keep only the fields that you want to, including in nested models.
288288

289289
That means that this way you can effortlessly create pydantic models for requests and responses in `fastapi`.
290290

291291
!!!Note
292-
To read more about possible excludes/includes and how to structure your exclude dictionary or set visit [fields](../queries/select-columns.md#fields) section of documentation
292+
To read more about possible excludes/includes and how to structure your exclude dictionary or set visit [fields](../queries/select-columns.md#fields) section of documentation
293293

294294
Given sample ormar models like follows:
295295

@@ -330,8 +330,8 @@ class Category(BaseModel):
330330
```
331331

332332
!!!warning
333-
Note that it's not a good practice to have several classes with same name in one module, as well as it would break `fastapi` docs.
334-
Thats's why ormar adds random 3 uppercase letters to the class name. In example above it means that in reality class would be named i.e. `Category_XIP(BaseModel)`.
333+
Note that it's not a good practice to have several classes with same name in one module, as well as it would break `fastapi` docs.
334+
Thats's why ormar adds random 3 uppercase letters to the class name. In example above it means that in reality class would be named i.e. `Category_XIP(BaseModel)`.
335335

336336
To exclude or include nested fields you can use dict or double underscores.
337337

@@ -351,19 +351,19 @@ class Category(BaseModel):
351351
items: Optional[List[Item]]
352352
```
353353

354-
Of course, you can use also deeply nested structures and ormar will generate it pydantic equivalent you (in a way that exclude loops).
354+
Of course, you can use also deeply nested structures and ormar will generate it's pydantic equivalent for you (in a way that exclude loops).
355355

356356
Note how `Item` model above does not have a reference to `Category` although in ormar the relation is bidirectional (and `ormar.Item` has `categories` field).
357357

358358
!!!warning
359-
Note that the generated pydantic model will inherit all **field** validators from the original `ormar` model, that includes the ormar choices validator as well as validators defined with `pydantic.validator` decorator.
360-
361-
But, at the same time all root validators present on `ormar` models will **NOT** be copied to the generated pydantic model. Since root validator can operate on all fields and a user can exclude some fields during generation of pydantic model it's not safe to copy those validators.
362-
If required, you need to redefine/ manually copy them to generated pydantic model.
359+
Note that the generated pydantic model will inherit all **field** validators from the original `ormar` model, that includes the ormar choices validator as well as validators defined with `pydantic.validator` decorator.
360+
361+
But, at the same time all root validators present on `ormar` models will **NOT** be copied to the generated pydantic model. Since root validator can operate on all fields and a user can exclude some fields during generation of pydantic model it's not safe to copy those validators.
362+
If required, you need to redefine/ manually copy them to generated pydantic model.
363363

364-
## load
364+
## load()
365365

366-
By default when you query a table without prefetching related models, the ormar will still construct
366+
By default, when you query a table without prefetching related models, the ormar will still construct
367367
your related models, but populate them only with the pk value. You can load the related model by calling `load()` method.
368368

369369
`load()` can also be used to refresh the model from the database (if it was changed by some other process).
@@ -378,14 +378,14 @@ await track.album.load()
378378
track.album.name # will return 'Malibu'
379379
```
380380

381-
## load_all
381+
## load_all()
382382

383383
`load_all(follow: bool = False, exclude: Union[List, str, Set, Dict] = None) -> Model`
384384

385385
Method works like `load()` but also goes through all relations of the `Model` on which the method is called,
386386
and reloads them from database.
387387

388-
By default the `load_all` method loads only models that are directly related (one step away) to the model on which the method is called.
388+
By default, the `load_all` method loads only models that are directly related (one step away) to the model on which the method is called.
389389

390390
But you can specify the `follow=True` parameter to traverse through nested models and load all of them in the relation tree.
391391

@@ -411,7 +411,7 @@ Method performs one database query so it's more efficient than nested calls to `
411411
!!!warning
412412
All relations are cleared on `load_all()`, so if you exclude some nested models they will be empty after call.
413413

414-
## save
414+
## save()
415415

416416
`save() -> self`
417417

@@ -430,7 +430,7 @@ track = await Track.objects.get(name='The Bird')
430430
await track.save() # will raise integrity error as pk is populated
431431
```
432432

433-
## update
433+
## update()
434434

435435
`update(_columns: List[str] = None, **kwargs) -> self`
436436

@@ -449,7 +449,7 @@ To update only selected columns from model into the database provide a list of c
449449

450450
In example:
451451

452-
```python
452+
```python hl_lines="16"
453453
class Movie(ormar.Model):
454454
ormar_config = base_ormar_config.copy()
455455

@@ -476,9 +476,9 @@ assert terminator.year == 1984
476476
```
477477

478478
!!!warning
479-
Note that `update()` does not refresh the instance of the Model, so if you change more columns than you pass in `_columns` list your Model instance will have different values than the database!
479+
Note that `update()` does not refresh the instance of the Model, so if you change more columns than you pass in `_columns` list your Model instance will have different values than the database!
480480

481-
## upsert
481+
## upsert()
482482

483483
`upsert(**kwargs) -> self`
484484

@@ -497,7 +497,7 @@ await track.upsert(name='The Bird Strikes Again') # will call update as pk is al
497497
```
498498

499499

500-
## delete
500+
## delete()
501501

502502
You can delete models by using `QuerySet.delete()` method or by using your model and calling `delete()` method.
503503

@@ -509,7 +509,7 @@ await track.delete() # will delete the model from database
509509
!!!tip
510510
Note that that `track` object stays the same, only record in the database is removed.
511511

512-
## save_related
512+
## save_related()
513513

514514
`save_related(follow: bool = False, save_all: bool = False, exclude=Optional[Union[Set, Dict]]) -> None`
515515

@@ -532,11 +532,11 @@ If you want to skip saving some of the relations you can pass `exclude` paramete
532532
or it can be a dictionary that can also contain nested items.
533533

534534
!!!note
535-
Note that `exclude` parameter in `save_related` accepts only relation fields names, so
536-
if you pass any other fields they will be saved anyway
535+
Note that `exclude` parameter in `save_related` accepts only relation fields names, so
536+
if you pass any other fields they will be saved anyway
537537

538538
!!!note
539-
To read more about the structure of possible values passed to `exclude` check `Queryset.fields` method documentation.
539+
To read more about the structure of possible values passed to `exclude` check `Queryset.fields` method documentation.
540540

541541
!!!warning
542542
To avoid circular updates with `follow=True` set, `save_related` keeps a set of already visited Models on each branch of relation tree,

0 commit comments

Comments
 (0)
0