Skip to content

Model methods

Tip

Main interaction with the databases is exposed through a QuerySet object exposed on each model as Model.objects similar to the django orm.

To read more about quering, joining tables, excluding fields etc. visit queries section.

Each model instance have a set of methods to save, update or load itself.

Available methods are described below.

pydantic methods

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

To read more check pydantic documentation

model_construct()

model_construct is a raw equivalent of __init__ method used for construction of new instances.

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

Note

Note that in contrast to pydantic.model_construct method - the ormar equivalent will also process the nested related models.

Warning

Bear in mind that due to skipped validation the construct method does not perform any conversions, checks etc. So it's your responsibility to provide that data that is valid and can be consumed by the database.

The only two things that construct still performs are:

  • Providing a default value for not set fields
  • Initialize nested ormar models if you pass a dictionary or a primary key value

model_dump()

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

model_dump as the name suggests export data from model tree to dictionary.

Options like mode, by_alias, exclude_unset, exclude_defaults, exclude_none, exclude_list and round_trip apply to the whole tree, including nested related and through models. A relation set to None is treated as a field equal to its default, so it is dropped by exclude_none and exclude_defaults, and by exclude_unset when it was not set explicitly. List relations (reverse and many to many) are always included unless exclude_list is set.

Explanation of model_dump parameters:

include (ormar modified)

include: Union[set, dict] = None

Set or dictionary of field names to include in returned dictionary.

Note that pydantic has an uncommon pattern of including/ excluding fields in lists (so also nested models) by an index. And if you want to exclude the field in all children you need to pass a __all__ key to dictionary.

You cannot exclude nested models in sets in pydantic but you can in ormar (by adding double underscore on relation name i.e. to exclude name of category for a book you can use exclude={"book__category__name"})

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

To check how you can include/exclude fields, including nested fields check out fields section that has an explanation and a lot of samples.

Note

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!

exclude (ormar modified)

exclude: Union[set, dict] = None

Set or dictionary of field names to exclude in returned dictionary.

Note that pydantic has an uncommon pattern of including/ excluding fields in lists (so also nested models) by an index. And if you want to exclude the field in all children you need to pass a __all__ key to dictionary.

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

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

To check how you can include/exclude fields, including nested fields check out fields section that has an explanation and a lot of samples.

Note

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!

exclude_unset

exclude_unset: bool = False

Flag indicates whether fields which were not explicitly set when creating the model should be excluded from the returned dictionary.

Warning

Note that after you save data into database each field has its own value -> either provided by you, default, or None.

That means that when you load the data from database, all fields are set, and this flag basically stop working!

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Category(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="categories")

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100, default="Test")
    visibility: bool = ormar.Boolean(default=True)


class Item(ormar.Model):
    ormar_config = base_ormar_config.copy()

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100)
    price: float = ormar.Float(default=9.99)
    categories: list[Category] = ormar.ManyToMany(Category)

category = Category(name="Test 2")
assert category.model_dump() == {'id': None, 'items': [], 'name': 'Test 2',
                           'visibility': True}
assert category.model_dump(exclude_unset=True) == {'items': [], 'name': 'Test 2'}

await category.save()
category2 = await Category.objects.get()
assert category2.model_dump() == {'id': 1, 'items': [], 'name': 'Test 2',
                            'visibility': True}
# NOTE how after loading from db all fields are set explicitly
# as this is what happens when you populate a model from db
assert category2.model_dump(exclude_unset=True) == {'id': 1, 'items': [],
                                              'name': 'Test 2', 'visibility': True}

exclude_defaults

exclude_defaults: bool = False

Flag indicates are equal to their default values (whether set or otherwise) should be excluded from the returned dictionary

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Category(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="categories")

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100, default="Test")
    visibility: bool = ormar.Boolean(default=True)

class Item(ormar.Model):
    ormar_config = base_ormar_config.copy()

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100)
    price: float = ormar.Float(default=9.99)
    categories: list[Category] = ormar.ManyToMany(Category)

category = Category()
# note that Integer pk is by default autoincrement so optional
assert category.model_dump() == {'id': None, 'items': [], 'name': 'Test', 'visibility': True}
assert category.model_dump(exclude_defaults=True) == {'items': []}

# save and reload the data
await category.save()
category2 = await Category.objects.get()

assert category2.model_dump() == {'id': 1, 'items': [], 'name': 'Test', 'visibility': True}
assert category2.model_dump(exclude_defaults=True) == {'id': 1, 'items': []}

exclude_none

exclude_none: bool = False

Flag indicates whether fields which are equal to None should be excluded from the returned dictionary.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Category(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="categories")

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100, default="Test", nullable=True)
    visibility: bool = ormar.Boolean(default=True)


class Item(ormar.Model):
    ormar_config = base_ormar_config.copy()

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100)
    price: float = ormar.Float(default=9.99)
    categories: list[Category] = ormar.ManyToMany(Category)


category = Category(name=None)
assert category.model_dump() == {'id': None, 'items': [], 'name': None,
                           'visibility': True}
# note the id is not set yet so None and excluded
assert category.model_dump(exclude_none=True) == {'items': [], 'visibility': True}

await category.save()
category2 = await Category.objects.get()
assert category2.model_dump() == {'id': 1, 'items': [], 'name': None,
                            'visibility': True}
assert category2.model_dump(exclude_none=True) == {'id': 1, 'items': [],
                                             'visibility': True}

exclude_primary_keys (ormar only)

exclude_primary_keys: bool = False

Setting flag to True will exclude all primary key columns in a tree, including nested models.

Python
1
2
3
4
5
6
7
8
9
class Item(ormar.Model):
    ormar_config = base_ormar_config.copy()

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100)

item1 = Item(id=1, name="Test Item")
assert item1.model_dump() == {"id": 1, "name": "Test Item"}
assert item1.model_dump(exclude_primary_keys=True) == {"name": "Test Item"}

exclude_through_models (ormar only)

exclude_through_models: bool = False

Through models are auto added for every ManyToMany relation, and they hold additional parameters on linking model/table.

Setting the exclude_through_models=True will exclude all through models, including Through models of submodels.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Category(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="categories")

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100)


class Item(ormar.Model):
    ormar_config = base_ormar_config.copy()

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100)
    categories: list[Category] = ormar.ManyToMany(Category)

# tree defining the models
item_dict = {
            "name": "test",
            "categories": [{"name": "test cat"}, {"name": "test cat2"}],
        }
# save whole tree
await Item(**item_dict).save_related(follow=True, save_all=True)

# get the saved values
item = await Item.objects.select_related("categories").get()

# by default you can see the through models (itemcategory)
assert item.model_dump() == {'id': 1, 'name': 'test', 
                       'categories': [
                           {'id': 1, 'name': 'test cat', 
                            'itemcategory': {'id': 1, 'category': None, 'item': None}}, 
                           {'id': 2, 'name': 'test cat2', 
                            'itemcategory': {'id': 2, 'category': None, 'item': None}}
                       ]}

# you can exclude those fields/ models
assert item.model_dump(exclude_through_models=True) == {
                       'id': 1, 'name': 'test', 
                       'categories': [
                           {'id': 1, 'name': 'test cat'}, 
                           {'id': 2, 'name': 'test cat2'}
                       ]}

model_dump_json()

model_dump_json() has exactly the same parameters as model_dump() so check above.

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

get_pydantic()

get_pydantic(include: Union[set, dict] = None, exclude: Union[set, dict] = None, flatten_fields: Union[set, list, str, tuple, dict, FieldAccessor] = None, flatten_all: bool = False, max_depth: Optional[int] = None, omit_back_relations: Optional[bool] = None)

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

Note that if you have nested models, it will generate the nested pydantic models for you! Each related model is generated once as a named nested model and relations point to it, so cyclic relations become recursive models ($ref in OpenAPI) instead of an endless tree.

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

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

Note

To read more about possible excludes/includes and how to structure your exclude dictionary or set visit fields section of documentation

Given sample ormar models like follows:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
base_ormar_config = ormar.OrmarConfig(
    metadata=sqlalchemy.MetaData(),
    database=DatabaseConnection(DATABASE_URL),
)


class Category(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="categories")

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100)


class Item(ormar.Model):
    ormar_config = base_ormar_config.copy()

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100, default="test")
    category: Optional[Category] = ormar.ForeignKey(Category, nullable=True)

You can generate pydantic models out of it with a one simple call.

Python
1
PydanticCategory = Category.get_pydantic(include={"id", "name"})

Which will generate model equivalent of:

Python
1
2
3
class Category(BaseModel):
    id: Optional[int]
    name: Optional[str] = "test"

Note

Generated models have stable names, so the OpenAPI document does not change between runs or with the order of get_pydantic() calls: Category for the model itself, Category_nested for the model nested in another one, Category_depth2 / Item_nested_depth1 with max_depth, and a short hash of the selector appended when include, exclude or flatten_fields are passed (e.g. Category_3f9a1c). Models with the same class name in different modules get a number suffix. Generated models are cached, so equal calls return the same class.

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

Python
1
2
3
# both calls are equivalent
PydanticCategory = Category.get_pydantic(include={"id", "items__id"})
PydanticCategory = Category.get_pydantic(include={"id": ..., "items": {"id"}})

and results in a generated structure as follows:

Python
1
2
3
4
5
6
class Item(BaseModel):
    id: Optional[int]

class Category(BaseModel):
    id: Optional[int]
    items: Optional[list[Item]]

Without include/exclude the nested model keeps its relations, also the one back to the parent, as optional fields pointing to the nested models of the related models:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
PydanticCategory = Category.get_pydantic()
# equivalent of:
class Category_nested(BaseModel):
    id: Optional[int] = None
    name: str
    items: Optional[list["Item_nested"]] = None

class Item_nested(BaseModel):
    id: Optional[int] = None
    name: Optional[str] = "test"
    category: Optional[Category_nested] = None

class Category(BaseModel):
    id: Optional[int] = None
    name: str
    items: Optional[list[Item_nested]] = None

Relation fields of nested models are always optional, as model_dump() stops where a model is already on the path (the items of a category nested in an item are not dumped). The root model keeps the required/optional rules of its own fields.

Two parameters change the nesting:

  • max_depth=N nests at most N levels of relations, deeper relations are left out (and the schema is not recursive). max_depth=1 gives one level of nested models without their relations.
  • omit_back_relations=True leaves the relation back to the parent out of nested models, so Item nested in Category.items has no category field. This generates one nested model per (model, relation) pair (Item_without_category_nested, the root is Category_without_back_relations), more models and a slower OpenAPI generation on densely related schemas.
Python
1
2
Category.get_pydantic(max_depth=1)  # items without their relations
Category.get_pydantic(omit_back_relations=True)  # items without category

A generated model validates an ormar instance through its model_dump(), so you can return ormar models from a fastapi endpoint with a get_pydantic() response model even when the loaded relations point back to each other.

Note

Until 0.30.0 nested models were a fixed-depth copy of the relation tree without the relation back to the parent, and with random names. The deprecated ORMAR_RELATION_TRAVERSAL=map switch keeps that shape for one release (unless max_depth or omit_back_relations is passed); it is removed in 0.31.0. Use max_depth=1 or omit_back_relations=True to keep a response close to the old shape.

Warning

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.

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. If required, you need to redefine/ manually copy them to generated pydantic model.

Methods decorated with @pydantic.computed_field are also carried over to the generated model and follow the same include / exclude rules as regular fields, so they appear in the model's schema and model_dump() output (e.g. in fastapi responses).

Relations as primary keys

By default, relations are generated as nested pydantic models. If you want a relation to be represented only by the primary key of the related model, pass it in the flatten_fields parameter, or use flatten_all=True to flatten every relation. Both work exactly like the same parameters of model_dump() and QuerySet.flatten_fields(), so the generated model describes the flattened output.

Python
1
PydanticItem = Item.get_pydantic(flatten_fields="category")

Which will generate model equivalent of:

Python
1
2
3
4
class Item(BaseModel):
    id: Optional[int]
    name: Optional[str] = "test"
    category: Optional[int] = None

The field type is the type of the related model's primary key (so it works with UUID or str primary keys as well). Many-to-many and reverse relations are generated as lists of primary keys.

Python
1
2
3
4
5
6
PydanticCategory = Category.get_pydantic(flatten_fields="items")
# equivalent of:
class Category(BaseModel):
    id: Optional[int]
    name: Optional[str]
    items: Optional[list[int]] = None

The generated field accepts a bare primary key, a dict or an ormar model and reduces it to the primary key value. That means that the same model can be used both for requests (where you send {"category": 1}) and for responses (where you can return ormar models directly, even if they were not loaded with flatten_fields).

Nested relations can be flattened as well, the parent relation stays a nested model. All input forms accepted by flatten_fields work (dunder strings, list/set/tuple, nested dict with Ellipsis and FieldAccessor):

Python
1
2
3
# position is a nested model, but item inside it is represented by its pk
PydanticOrder = Order.get_pydantic(flatten_fields="position__item")
PydanticOrder = Order.get_pydantic(flatten_fields=Order.position.item)

Note

The same validation rules as in model_dump() apply: flattening a non-relation field, an unknown relation or a relation with sub-fields selected in include/exclude raises QueryDefinitionError. Excluding the whole relation is fine.

load()

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

load() can also be used to refresh the model from the database (if it was changed by some other process).

Python
1
2
3
4
5
6
7
track = await Track.objects.get(name='The Bird')
track.album.pk # will return malibu album pk (1)
track.album.name # will return None

# you need to actually load the data first
await track.album.load()
track.album.name # will return 'Malibu'

load_all()

load_all(follow: bool = False, exclude: Union[list, str, set, dict] = None, order_by: Union[list, str] = None, max_depth: Optional[int] = None, follow_reverse: Union[bool, int] = 0) -> Model

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

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.

But you can specify the follow=True parameter to traverse through nested models. Like select_all(follow=True) it follows foreign keys (nullable ones too) up to max_depth levels (default 5), and reverse foreign keys and many to many relations only in the first follow_reverse levels (True = 1, default 0).

Python
1
2
await department.load_all(follow=True, follow_reverse=2)  # courses and their students
await chapter.load_all(follow=True, max_depth=2)  # book and its author

Warning

To avoid circular loads with follow=True set, a relation is not followed when its target model is already on the path above (a direct self relation is followed once).

So if you have a diamond or circular relations types you need to perform the loads in a manual way.

Python
1
2
# in example like this the second Street (coming from City) won't be followed, so ZipCode won't be reloaded
Street -> District -> City -> Street -> ZipCode

Method accepts also optional exclude parameter that works exactly the same as exclude_fields method in QuerySet. That way you can remove fields from related models being refreshed or skip whole related models.

Method performs one database query so it's more efficient than nested calls to load() and all() on related models.

Tip

To read more about exclude read exclude_fields

Warning

All relations are cleared on load_all(), so if you exclude some nested models they will be empty after call.

save()

save() -> self

You can create new models by using QuerySet.create() method or by initializing your model as a normal pydantic model and later calling save() method.

save() can also be used to persist changes that you made to the model, but only if the primary key is not set or the model does not exist in database.

The save() method does not check if the model exists in db, so if it does you will get a integrity error from your selected db backend if trying to save model with already existing primary key.

Python
1
2
3
4
5
track = Track(name='The Bird')
await track.save() # will persist the model in database

track = await Track.objects.get(name='The Bird')
await track.save() # will raise integrity error as pk is populated

update()

update(_columns: list[str] = None, _partial: bool = None, **kwargs) -> self

You can update models by using QuerySet.update() method or by updating your model attributes (fields) and calling update() method.

If you try to update a model without a primary key set a ModelPersistenceError exception will be thrown.

To persist a newly created model use save() or upsert(**kwargs) methods.

Python
1
2
track = await Track.objects.get(name='The Bird')
await track.update(name='The Bird Strikes Again')

Which columns are written

With partial_updates=True in the model config update() writes only what changed since the model was loaded or saved:

  • the fields passed as kwargs,
  • the fields assigned on the instance (track.name = "x", also in pre_update signal handlers and relations assigned with track.album = album; a foreign key writes all of its columns, also the ones it shares with other fields),
  • Json fields mutated in place (track.settings["volume"] = 5), detected by comparing with a copy taken when the row is loaded or saved,
  • fields with on_update.

If nothing changed, all columns are written, as before. The tracking is cleared after load(), load_all(), save(), update() and QuerySet.bulk_update().

_partial=True / _partial=False overrides the config for one call: False writes all columns of the model.

Python
1
2
3
4
track = await Track.objects.get(name='The Bird')
track.position = 3
# UPDATE tracks SET name=..., position=... WHERE id = ...
await track.update(name='The Bird Strikes Again')

Lost updates

Without partial_updates=True (the default is None), update(**kwargs) writes every column of the model from memory, not only the passed ones. If the row was changed in the database after the instance was loaded (by another request or a background worker), those changes are silently reverted:

Python
1
2
3
4
stale = await Training.objects.get(id=1)
# ... meanwhile another request runs:
#     await (await Training.objects.get(id=1)).update(seats_taken=5)
await stale.update(status="closed")  # also writes seats_taken=0 back!

With the default config this call emits a DeprecationWarning whenever other columns are written as well. Set partial_updates=True (or pass _columns) to write only what you changed; partial_updates=False keeps writing all columns and silences the warning. The default will change to partial updates in a future release.

Partial updates write less, but they do not lock the row: two requests changing the same column still race, use a transaction with row locking or an atomic QuerySet.update() for counters.

Selected columns

To update only selected columns from model into the database provide a list of columns that should be updated to _columns argument (fields with on_update are written too).

In example:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Movie(ormar.Model):
    ormar_config = base_ormar_config.copy()

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100, nullable=False, name="title")
    year: int = ormar.Integer()
    profit: float = ormar.Float()

terminator = await Movie(name='Terminator', year=1984, profit=0.078).save()

terminator.name = "Terminator 2"
terminator.year = 1991
terminator.profit = 0.520

# update only name
await terminator.update(_columns=["name"])

# note that terminator instance was not reloaded so
assert terminator.year == 1991

# but once you load the data from db you see it was not updated
await terminator.load()
assert terminator.year == 1984

Warning

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!

upsert()

upsert(**kwargs) -> self

It's a proxy to either save() or update(**kwargs) methods described above:

  • a model loaded from the database or already saved -> update(**kwargs),
  • a model without a primary key -> save(),
  • a model never saved but with its key set (uuid default, natural or composite key) is looked up by key first -> update(**kwargs) if the row exists, save() otherwise.

See primary keys for details.

Python
1
2
3
4
5
track = Track(name='The Bird')
await track.upsert() # will call save as the pk is empty

track = await Track.objects.get(name='The Bird')
await track.upsert(name='The Bird Strikes Again') # will call update as pk is already populated

delete()

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

Python
1
2
track = await Track.objects.get(name='The Bird')
await track.delete() # will delete the model from database

Tip

Note that that track object stays the same, only record in the database is removed.

save_related(follow: bool = False, save_all: bool = False, exclude=Optional[Union[set, dict]]) -> None

Method goes through all relations of the Model on which the method is called, and calls upsert() method on each model that is not saved.

To understand when a model is saved check save status section above.

By default the save_related method saved only models that are directly related (one step away) to the model on which the method is called.

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

By default save_related saves only model that has not saved status, meaning that they were modified in current scope.

If you want to force saving all of the related methods use save_all=True flag, which will upsert all related models, regardless of their save status.

If you want to skip saving some of the relations you can pass exclude parameter.

Exclude can be a set of own model relations, or it can be a dictionary that can also contain nested items.

Note

Note that exclude parameter in save_related accepts only relation fields names, so if you pass any other fields they will be saved anyway

Note

To read more about the structure of possible values passed to exclude check Queryset.fields method documentation.

Warning

To avoid circular updates with follow=True set, save_related keeps a set of already visited Models on each branch of relation tree, and won't perform nested save_related on Models that were already visited.

So if you have circular relations types you need to perform the updates in a manual way.

Note that with save_all=True and follow=True you can use save_related() to save whole relation tree at once.

Example:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Department(ormar.Model):
    ormar_config = base_ormar_config.copy()

    id: int = ormar.Integer(primary_key=True)
    department_name: str = ormar.String(max_length=100)


class Course(ormar.Model):
    ormar_config = base_ormar_config.copy()

    id: int = ormar.Integer(primary_key=True)
    course_name: str = ormar.String(max_length=100)
    completed: bool = ormar.Boolean()
    department: Optional[Department] = ormar.ForeignKey(Department)


class Student(ormar.Model):
    ormar_config = base_ormar_config.copy()

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100)
    courses = ormar.ManyToMany(Course)

to_save = {
            "department_name": "Ormar",
            "courses": [
                {"course_name": "basic1",
                 "completed": True,
                 "students": [
                     {"name": "Jack"},
                     {"name": "Abi"}
                 ]},
                {"course_name": "basic2",
                 "completed": True,
                 "students": [
                     {"name": "Kate"},
                     {"name": "Miranda"}
                 ]
                 },
            ],
        }
# initialize whole tree
department = Department(**to_save)

# save all at once (one after another)
await department.save_related(follow=True, save_all=True)

department_check = await Department.objects.select_all(follow=True).get()

to_exclude = {
    "id": ...,
    "courses": {
        "id": ...,
        "students": {"id", "studentcourse"}
    }
}
# after excluding ids and through models you get exact same payload used to
# construct whole tree
assert department_check.model_dump(exclude=to_exclude) == to_save

Warning

save_related() iterates all relations and all models and upserts() them one by one, so it will save all models but might not be optimal in regard of number of database queries.