Skip to content

# Modeling

## Models

To create a Model, simply inherit from the Model class and then specify the field types and eventually their descriptors.

### Collection

Each Model will be linked to its own collection. By default, the collection name will be created from the chosen class name and converted to snake_case. For example a model class named `CapitalCity` will be stored in the collection named `capital_city`.

If the class name ends with `Model`, ODMantic will remove it to create the collection name. For example, a model class named `PersonModel` will belong in the `person` collection.

It's possible to customize the collection name of a model by specifying the `collection` option in the `model_config` class attribute.

Custom collection name example

    from odmantic import Model

    class CapitalCity(Model):
        name: str
        population: int

        model_config = {
            "collection": "city"
        }

Now, when `CapitalCity` instances will be persisted to the database, they will belong in the `city` collection instead of `capital_city`.

Warning

Models and Embedded models inheritance is not supported yet.

### Indexes

#### Index definition

There are two ways to create indexes on a model in ODMantic. The first one is to use the Field descriptors as explained in Indexed fields or Unique fields. However, this way doesn't allow a great flexibility on index definition.

That's why you can also use the `model_config.indexes` generator to specify advanced indexes (compound indexes, custom names). This static function defined in the `model_config` class attribute should yield odmantic.Index.

For example:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15

    from odmantic import Field, Index, Model

    class Product(Model):
        name: str = Field(index=True)
        stock: int
        category: str
        sku: str = Field(unique=True)

        model_config = {
            "indexes": lambda: [
                Index(Product.name, Product.stock, name="name_stock_index"),
                Index(Product.name, Product.category, unique=True),
            ]
        }

This snippet creates 4 indexes on the `Product` model:

- An index on the `name` field defined with the field descriptor, improving lookup performance by product name.

- A unique index on the `sku` field defined with the field descriptor, enforcing uniqueness of the `sku` field.

- A compound index on the `name` and `stock` fields, making sure the following query will be efficient (i.e. avoid a full collection scan):

      engine.find(Product, Product.name == "banana", Product.stock > 5)

- A unique index on the `name` and `category` fields, making sure each category has unique product name.

Sort orders with index definition

You can also specify the sort order of the fields in the index definition using query.asc and query.desc as presented in the Sorting section.

For example defining the following index on the `Event` model:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11

    from datetime import datetime

    from odmantic import Index, Model
    from odmantic.query import asc, desc

    class Event(Model):
        username: str
        date: datetime

        model_config = {"indexes": lambda: [Index(asc(Event.username), desc(Event.date))]}

Will greatly improve the performance of the query:

    engine.find(Event, sort=(asc(Event.name), desc(Event.date))

#### Index creation

In order to create and enable the indexes in the database, you need to call the `engine.configure_database` method (either AIOEngine.configure_database or SyncEngine.configure_database).

Async Sync

    1
    2
    3
    4
    5
    6

    # ... Continuation of the previous snippet ...

    from odmantic import AIOEngine

    engine = AIOEngine()
    await engine.configure_database([Product])

    1
    2
    3
    4
    5
    6

    # ... Continuation of the previous snippet ...

    from odmantic import SyncEngine

    engine = SyncEngine()
    engine.configure_database([Product])

This method can also take a `update_existing_indexes=True` parameter to update existing indexes when the index definition changes. If not enabled, an exception will be thrown when a conflicting index update happens.

#### Advanced indexes

In some cases, you might need a greater flexibility on the index definition (Geo2D, Hashed, Text indexes for example), the `Config.indexes` generator can also yield pymongo.IndexModel objects.

For example, defining a text index :

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16

    import pymongo

    from odmantic import Model

    class Post(Model):
        title: str
        content: str

        model_config = {
            "indexes": lambda: [
                pymongo.IndexModel(
                    [(+Post.title, pymongo.TEXT), (+Post.content, pymongo.TEXT)]
                )
            ]
        }

### Custom model validators

Exactly as done with pydantic, it's possible to define custom model validators as described in the pydantic: Root Validators documentation (this apply as well to Embedded Models).

In the following example, we will define a rectangle class and add two validators: The first one will check that the height is greater than the width. The second one will ensure that the area of the rectangle is less or equal to 9.

     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
    60
    61

    from typing import ClassVar

    from pydantic import ValidationError, model_validator

    from odmantic import Model

    class SmallRectangle(Model):
        MAX_AREA: ClassVar[float] = 9

        length: float
        width: float

        @model_validator(mode="before")
        def check_width_length(cls, values):
            length = values.get("length", 0)
            width = values.get("width", 0)
            if width > length:
                raise ValueError("width can't be greater than length")
            return values

        @model_validator(mode="before")
        def check_area(cls, values):
            length = values.get("length", 0)
            width = values.get("width", 0)
            if length * width > cls.MAX_AREA:
                raise ValueError(f"area is greater than {cls.MAX_AREA}")
            return values

    print(SmallRectangle(length=2, width=1))
    # > id=ObjectId('5f81e3c073103f509f97e374'), length=2.0, width=1.0

    try:
        SmallRectangle(length=2)
    except ValidationError as e:
        print(e)
        """
        1 validation error for SmallRectangle
        width
          field required (type=value_error.missing)
        """

    try:
        SmallRectangle(length=2, width=3)
    except ValidationError as e:
        print(e)
        """
        1 validation error for SmallRectangle
          Value error, width can't be greater than length
        """

    try:
        SmallRectangle(length=4, width=3)
    except ValidationError as e:
        print(e)
        """
        1 validation error for SmallRectangle
        __root__
          Value error, area is greater than 9
        """

Tip

You can define class variables in the Models using the `typing.ClassVar` type construct, as done in this example with `MAX_AREA`. Those class variables will be completely ignored by ODMantic while persisting instances in the database.

### Advanced Configuration

The model configuration is done in the same way as with Pydantic models: using a ConfigDict `model_config` defined in the model body.

Here is an example of a model configuration:

    class Event(Model):
        date: datetime

        model_config = {
            "collection": "event_collection",
            "parse_doc_with_default_factories": True,
            "indexes": lambda: [
                Index(Event.date, unique=True),
                pymongo.IndexModel([("date", pymongo.DESCENDING)]),
            ],
        }

#### Available options

`collection: str`
  Customize the collection name associated to the model. See this section for more details about default collection naming.
`parse_doc_with_default_factories: bool`

  Wether to allow populating field values with default factories while parsing documents from the database. See Advanced parsing behavior for more details.

  Default: `False`

`indexes: Callable[[],Iterable[Union[Index, pymongo.IndexModel]]]`

  Define additional indexes for the model. See Indexes for more details.

  Default: `lambda: []`

`title: str` (inherited from Pydantic)

  Title inferred in the JSON schema.

  Default: name of the model class

`schema_extra: dict` (inherited from Pydantic)

  A dict used to extend/update the generated JSON Schema, or a callable to post-process it. See Pydantic: Schema customization for more details.

  Default: `{}`

`anystr_strip_whitespace: bool` (inherited from Pydantic)

  Whether to strip leading and trailing whitespaces for str & byte types.

  Default: `False`

`json_encoders: dict` (inherited from Pydantic)

  Customize the way types used in the model are encoded to JSON.

  `json_encoders` example

  For example, in order to serialize `datetime` fields as timestamp values:

      class Event(Model):
          date: datetime

          model_config = {
              "json_encoders": {
                  datetime: lambda v: v.timestamp()
              }
          }

`extra: pydantic.Extra` (inherited from Pydantic)

  Whether to ignore, allow, or forbid extra attributes during model initialization. Accepts the string values of 'ignore', 'allow', or 'forbid', or values of the Extra enum. 'forbid' will cause validation to fail if extra attributes are included, 'ignore' will silently ignore any extra attributes, and 'allow' will assign the attributes to the model, reflecting them in the saved database documents and fetched instances.

  Default: `Extra.ignore`

For more details and examples about the options inherited from Pydantic, you can have a look at Pydantic: Model Config

Warning

Only the options described above are supported and other options from Pydantic can't be used with ODMantic.

If you feel the need to have an additional option inherited from Pydantic, you can open an issue.

## Embedded Models

Using an embedded model will store it directly in the root model it's integrated in. On the MongoDB side, the collection will contain the root documents and in inside each of them, the embedded models will be directly stored.

Embedded models are especially useful while building one-to-one or one-to-many relationships.

Note

Since Embedded Models are directly embedded in the MongoDB collection of the root model, it will not be possible to query on them directly without specifying a root document.

The creation of an Embedded model is done by inheriting the EmbeddedModel class. You can then define fields exactly as for the regular Models.

### One to One

In this example, we will model the relation between a country and its capital city. Since one capital city can belong to one and only one country, we can model this relation as a One-to-One relationship. We will use an Embedded Model in this case.

     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

    from odmantic import AIOEngine, EmbeddedModel, Model

    class CapitalCity(EmbeddedModel):
        name: str
        population: int

    class Country(Model):
        name: str
        currency: str
        capital_city: CapitalCity

    countries = [
        Country(
            name="Switzerland",
            currency="Swiss franc",
            capital_city=CapitalCity(name="Bern", population=1035000),
        ),
        Country(
            name="Sweden",
            currency="Swedish krona",
            capital_city=CapitalCity(name="Stockholm", population=975904),
        ),
    ]

    engine = AIOEngine()
    await engine.save_all(countries)

Defining this relation is done in the same way as defining a new field. Here, the `CapitalCity` class will be considered as a field type during the model definition.

The Field descriptor can be used as well for Embedded Models in order to bring more flexibility (default values, Mongo key name, ...).

Content of the `country` collection after execution

    {
      "_id": ObjectId("5f79d7e8b305f24ca43593e2"),
      "name": "Sweden",
      "currency": "Swedish krona",
      "capital_city": {
        "name": "Stockholm",
        "population": 975904
      }
    }
    {
      "_id": ObjectId("5f79d7e8b305f24ca43593e1"),
      "name": "Switzerland",
      "currency": "Swiss franc",
      "capital_city": {
        "name": "Bern",
        "population": 1035000
      }
    }

Tip

It is possible as well to define query filters based on embedded documents content.

    await engine.find_one(
        Country, Country.capital_city.name == "Stockholm"
    )
    #> Country(
    #>     id=ObjectId("5f79d7e8b305f24ca43593e2"),
    #>     name="Sweden",
    #>     currency="Swedish krona",
    #>     capital_city=CapitalCity(name="Stockholm", population=975904),
    #> )

For more details, see the Querying section.

### One to Many

Here, we will model the relation between a customer of an online shop and his shipping addresses. A single customer can have multiple addresses but these addresses belong only to the customer's account. He should be allowed to modify them without modifying others addresses (for example if two family members use the same address, their addresses should not be linked together).

     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

    from typing import List

    from odmantic import AIOEngine, EmbeddedModel, Model

    class Address(EmbeddedModel):
        street: str
        city: str
        state: str
        zipcode: str

    class Customer(Model):
        name: str
        addresses: List[Address]

    customer = Customer(
        name="John Doe",
        addresses=[
            Address(
                street="1757  Birch Street",
                city="Greenwood",
                state="Indiana",
                zipcode="46142",
            ),
            Address(
                street="262  Barnes Avenue",
                city="Cincinnati",
                state="Ohio",
                zipcode="45216",
            ),
        ],
    )

    engine = AIOEngine()
    await engine.save(customer)

As done previously for the One to One relation, defining a One to Many relationship with Embedded Models is done exactly as defining a field with its type being a sequence of `Address` objects.

Content of the `customer` collection after execution

    {
      "_id": ObjectId("5f79eb116371e09b16e4fae4"),
      "name":"John Doe",
      "addresses":[
        {
          "street":"1757  Birch Street",
          "city":"Greenwood",
          "state":"Indiana",
          "zipcode":"46142"
        },
        {
          "street":"262  Barnes Avenue",
          "city":"Cincinnati",
          "state":"Ohio",
          "zipcode":"45216"
        }
      ]
    }

Tip

To add conditions on the number of embedded elements, it's possible to use the `min_items` and `max_items` arguments of the Field descriptor. Another possibility is to use the `typing.Tuple` type.

Note

Building query filters based on the content of a sequence of embedded documents is not supported yet (but this feature is planned for an upcoming release ).

Anyway, it's still possible to perform the filtering operation manually using Mongo Array Operators (`$all`, `$elemMatch`, `$size`). See the Raw query usage section for more details.

### Customization

Since the Embedded Models are considered as types by ODMantic, most of the complex type constructs that could be imagined should be supported.

Some ideas which could be useful:

- Combine two different embedded models in a single field using `typing.Tuple`.

- Allow multiple Embedded model types using a `typing.Union` type.

- Make an Embedded model not required using `typing.Optional`.

- Embed the documents in a dictionary (using the `typing.Dict` type) to provide an additional key-value mapping to the embedded documents.

- Nest embedded documents

## Referenced models

Embedded models are really simple to use but sometimes it is needed as well to have many-to-one (i.e. multiple entities referring to another single one) or many-to-many relationships. This is not really possible to model those using embedded documents and in this case, references will come handy. Another use case where references are useful is for one-to-one/one-to-many relations but when the referenced model has to exist in its own collection, in order to be accessed on its own without any parent model specified.

### Many to One (Mapped)

In this part, we will model the relation between books and publishers. Let's consider that each book has a single publisher. In this case, multiple books could be published by the same publisher. We can thus model this relation as a many-to-one relationship.

     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

    from odmantic import AIOEngine, Model, Reference

    class Publisher(Model):
        name: str
        founded: int
        location: str

    class Book(Model):
        title: str
        pages: int
        publisher: Publisher = Reference()

    hachette = Publisher(name="Hachette Livre", founded=1826, location="FR")
    harper = Publisher(name="HarperCollins", founded=1989, location="US")

    books = [
        Book(title="They Didn't See Us Coming", pages=304, publisher=hachette),
        Book(title="This Isn't Happening", pages=256, publisher=hachette),
        Book(title="Prodigal Summer", pages=464, publisher=harper),
    ]

    engine = AIOEngine()
    await engine.save_all(books)

The definition of a reference field requires the presence of the Reference() descriptor. Once the models are defined, linking two instances is done simply by assigning the reference field of the referencing instance to the referenced instance.

Why is it required to include the Reference descriptor ?

The main goal behind enforcing the presence of the descriptor is to have a clear distinction between Embedded Models and References.

In the future, a generic `Reference[T]` type will probably be included to make this distinction since it would make more sense than having to set a descriptor for each reference.

Content of the `publisher` collection after execution

    {
      "_id": ObjectId("5f7a0dc48a73b20f16e2a364"),
      "founded": 1826,
      "location": "FR",
      "name": "Hachette Livre"
    }
    {
      "_id": ObjectId("5f7a0dc48a73b20f16e2a365"),
      "founded": 1989,
      "location": "US",
      "name": "HarperCollins"
    }

We can see that the publishers have been persisted to their collection even if no explicit save has been performed. When calling the engine.save method, the engine will persist automatically the referenced documents.

While fetching instances, the engine will as well resolve every reference.

Content of the `book` collection after execution

    {
      "_id": ObjectId("5f7a0dc48a73b20f16e2a366"),
      "pages": 304,
      "publisher": ObjectId("5f7a0dc48a73b20f16e2a364"),
      "title": "They Didn't See Us Coming"
    }
    {
      "_id": ObjectId("5f7a0dc48a73b20f16e2a367"),
      "pages": 256,
      "publisher": ObjectId("5f7a0dc48a73b20f16e2a364"),
      "title": "This Isn't Happening"
    }
    {
      "_id": ObjectId("5f7a0dc48a73b20f16e2a368"),
      "pages": 464,
      "publisher": ObjectId("5f7a0dc48a73b20f16e2a365"),
      "title": "Prodigal Summer"
    }

The resulting books in the collection contain the publisher reference directly as a document attribute (using the reference name as the document's key).

Tip

It's possible to customize the foreign key storage key using the `key_name` argument while building the Reference descriptor.

### Many to Many (Manual)

Here, we will model the relation between books and their authors. Since a book can have multiple authors and an author can be authoring multiple books, we will model this relation as a many-to-many relationship.

Note

Currently, ODMantic does not support mapped multi-references yet. But we will still define the relationship in a manual way.

     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

    from typing import List

    from bson import ObjectId

    from odmantic import AIOEngine, Model

    class Author(Model):
        name: str

    class Book(Model):
        title: str
        pages: int
        author_ids: List[ObjectId]

    david = Author(name="David Beazley")
    brian = Author(name="Brian K. Jones")

    python_cookbook = Book(
        title="Python Cookbook", pages=706, author_ids=[david.id, brian.id]
    )
    python_essentials = Book(
        title="Python Essential Reference", pages=717, author_ids=[brian.id]
    )

    engine = AIOEngine()
    await engine.save_all((david, brian))
    await engine.save_all((python_cookbook, python_essentials))

We defined an `author_ids` field which holds the list of unique ids of the authors (This `id` field in the `Author` model is generated implicitly by default).

Since this multi-reference is not mapped by the ODM, we have to persist the authors manually.

Content of the `author` collection after execution

    {
      "_id": ObjectId("5f7a37dc7311be1362e1da4e"),
      "name": "David Beazley"
    }
    {
      "_id": ObjectId("5f7a37dc7311be1362e1da4f"),
      "name": "Brian K. Jones"
    }

Content of the `book` collection after execution

    {
      "_id": ObjectId("5f7a37dc7311be1362e1da50"),
      "title":"Python Cookbook"
      "pages":706,
      "author_ids":[
        ObjectId("5f7a37dc7311be1362e1da4e"),
        ObjectId("5f7a37dc7311be1362e1da4f")
      ],
    }
    {
      "_id": ObjectId("5f7a37dc7311be1362e1da51"),
      "title":"Python Essential Reference"
      "pages":717,
      "author_ids":[
        ObjectId("5f7a37dc7311be1362e1da4f")
      ],
    }

Retrieving the authors of the Python Cookbook

First, it's required to fetch the ids of the authors. Then we can use the in_ filter to select only the authors with the desired ids.

    1
    2
    3
    4
    5
    6
    7

    book = await engine.find_one(Book, Book.title == "Python Cookbook")
    authors = await engine.find(Author, Author.id.in_(book.author_ids))
    print(authors)
    #> [
    #>   Author(id=ObjectId("5f7a37dc7311be1362e1da4e"), name="David Beazley"),
    #>   Author(id=ObjectId("5f7a37dc7311be1362e1da4f"), name="Brian K. Jones"),
    #> ]