You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/models/methods.md
+47-47Lines changed: 47 additions & 47 deletions
Original file line number
Diff line number
Diff line change
@@ -13,37 +13,37 @@ Available methods are described below.
13
13
## `pydantic` methods
14
14
15
15
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.
17
17
18
18
To read more check [pydantic][pydantic] documentation
19
19
20
-
## construct
20
+
## model_construct()
21
21
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.
23
23
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.
25
25
The benefit of using construct is the speed of execution due to skipped validation.
26
26
27
27
!!!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.
29
29
30
30
!!!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:
35
35
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
38
38
39
-
## dict
39
+
## model_dump()
40
40
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,
42
42
therefore it's listed here for clarity.
43
43
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.
45
45
46
-
Explanation of dict parameters:
46
+
Explanation of model_dump parameters:
47
47
48
48
### include (`ormar` modified)
49
49
@@ -55,14 +55,14 @@ Note that `pydantic` has an uncommon pattern of including/ excluding fields in l
55
55
And if you want to exclude the field in all children you need to pass a `__all__` key to dictionary.
56
56
57
57
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"}`)
59
59
60
60
`ormar` does not support by index exclusion/ inclusions and accepts a simplified and more user-friendly notation.
61
61
62
62
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.
63
63
64
64
!!!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!
66
66
67
67
### exclude (`ormar` modified)
68
68
@@ -81,7 +81,7 @@ You cannot exclude nested models in `Set`s in `pydantic` but you can in `ormar`
81
81
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.
82
82
83
83
!!!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!
85
85
86
86
### exclude_unset
87
87
@@ -90,9 +90,9 @@ To check how you can include/exclude fields, including nested fields check out [
90
90
Flag indicates whether fields which were not explicitly set when creating the model should be excluded from the returned dictionary.
91
91
92
92
!!!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!
This method allows you to generate `pydantic` models from your ormar models without you needing to retype all the fields.
284
284
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.
286
286
287
287
Moreover, you can pass `exclude` and/or `include` parameters to keep only the fields that you want to, including in nested models.
288
288
289
289
That means that this way you can effortlessly create pydantic models for requests and responses in `fastapi`.
290
290
291
291
!!!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
293
293
294
294
Given sample ormar models like follows:
295
295
@@ -330,8 +330,8 @@ class Category(BaseModel):
330
330
```
331
331
332
332
!!!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)`.
335
335
336
336
To exclude or include nested fields you can use dictor double underscores.
337
337
@@ -351,19 +351,19 @@ class Category(BaseModel):
351
351
items: Optional[List[Item]]
352
352
```
353
353
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).
355
355
356
356
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).
357
357
358
358
!!!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.
363
363
364
-
## load
364
+
## load()
365
365
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
367
367
your related models, but populate them only with the pk value. You can load the related model by calling `load()` method.
368
368
369
369
`load()` can also be used to refresh the model from the database (if it was changed by some other process).
Note that `update()` does not refresh the instance of the Model, so if you change more columns than you passin`_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 passin`_columns`list your Model instance will have different values than the database!
480
480
481
-
## upsert
481
+
## upsert()
482
482
483
483
`upsert(**kwargs) ->self`
484
484
@@ -497,7 +497,7 @@ await track.upsert(name='The Bird Strikes Again') # will call update as pk is al
497
497
```
498
498
499
499
500
-
## delete
500
+
## delete()
501
501
502
502
You can delete models by using `QuerySet.delete()` method or by using your model and calling `delete()` method.
503
503
@@ -509,7 +509,7 @@ await track.delete() # will delete the model from database
509
509
!!!tip
510
510
Note that that `track`object stays the same, only record in the database is removed.
0 commit comments