Skip to main content

Pydantic Mongo ORM

Pydantic Mongo ORM is a library that allows users to map Pydantic models to MongoDb.

Installation

pip install pydantic_mongo_orm

Usage

Models

PMO uses Pydantic models to represent data in database. To start using it, you need to create new model based on pydantic_mongo_orm.BaseModel. This class implements all the features from Pydantic but also adds some orm-specific functionality, e.g. adds read-only field id that represents document-id (_id) from MongoDb:

from pydantic_mongo_orm import BaseModel

class MyModel(BaseModel):
    greeting: str

my_model = MyModel(greeting='Hello World!')

print(my_model.greeting)  # >> 'Hello World!'
print(my_model.id)  # >> uuid.UUID4('2daaa0ac-501b-4018-9101-cc822a37c1a1')

You may also specify mongo_config for each model class that further modifies ORM behaviour:

from pydantic_mongo_orm import BaseModel, MongoConfigDict

class MyModel(BaseModel):
    greeting: str

    mongo_config = MongoConfigDict(collection='my_model')

Available options are:

  • collection: Name of the collection where the model will be stored. By default, it's detected from the class name.
  • objects_class: Class that will be used as an object storage for given model. By default, ObjectsStorage is used. Explained later

Storage

The base of PMO is Storage - class connected to MongoDb able to map models to objects and vice-versa.

Connect to MongoDb and save new instance of MyModel from previous example:

from pydantic_mongo_orm import Storage

storage = Storage(host='mongodb://127.0.0.1:27017', db_name='local')
storage.connect()

my_model = MyModel(greeting='Hello World!')
storage.save(my_model)

my_models = storage.find(MyModel, {'greeting': 'Hello World!'})
print(list(my_models))  # [MyModel greetings: 'Hello World!']

Bound objects

You may use Storage object directly but PMO offers a shorthand that simplifies working with the ORM. Each model class provides attribute objects that provides direct access to collection storing that particular model. To enable this feature, you must first bind the Storage to the BaseModel using .bind(). This example is equivalent with the previous one:

from pydantic_mongo_orm import Storage

storage = Storage(host='mongodb://127.0.0.1:27017', db_name='local')
storage.connect()
storage.bind()

my_model = MyModel(greeting='Hello World!')
my_model.save()

my_models = MyModel.objects.find({'greeting': 'Hello World!'})
print(list(my_models))  # [MyModel greetings: 'Hello World!']

By default, the objects attribute contains an instance of pydantic_mongo_orm.ObjectsStorage. It provides the same functionality as pydantic_mongo_orm.Storage. If you wish to customize behaviour of certain model - change how it's stored, add custom filters etc., you may extend the ObjectsStorage class, add new functionality and then configure it to be used for your models:

from pydantic_mongo_orm import BaseModel, ObjectsStorage, Storage, MongoConfigDict


class RectangleStorage(ObjectsStorage):
    def squares(self):
        return self.find({'$where': 'this.x == this.y'})

    
class Rectangle(BaseModel):
    mongo_config = MongoConfigDict(objects_class=RectangleStorage)

    length: int
    width: int

    
storage = Storage(host='mongodb://127.0.0.1:27017', db_name='local')
storage.connect()
storage.bind()

my_square = Rectangle(x=5, y=5)
my_square.save()

my_rectangle = Rectangle(x=5, y=10)
my_rectangle.save()

print(list(Rectangle.objects.squares()))  # >> [Rectangle x=5 y=5]

Metadata

Release files for pydantic_mongo_orm 0.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic_mongo_orm 0.0.0
File Size Uploaded Provenance Actions
pydantic_mongo_orm-0.0.0.tar.gz 4.9 kB Without provenance Details for pydantic_mongo_orm-0.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pydantic_mongo_orm 0.0.0
File Interpreter ABI Platform Actions
pydantic_mongo_orm-0.0.0-py3-none-any.whl Python 3 none any Details for pydantic_mongo_orm-0.0.0-py3-none-any.whl

Total release size: 9.8 kB

Release files / pydantic_mongo_orm-0.0.0.tar.gz

Download URL pydantic_mongo_orm-0.0.0.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a236cca7d6f4d9451d7936310fd58b63bbf5167be25619b706173e1ca551d5c6
BLAKE2b-256 checksum
How to use checksums
3e13d4c986c16a241c58d26e029a496f5dff9e24f6ac0f3843e9c114cabc3f4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.0 Darwin/23.1.0

Release files / pydantic_mongo_orm-0.0.0-py3-none-any.whl

Download URL pydantic_mongo_orm-0.0.0-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3ed8287acdb67f4f972ae39e735d2b6a2bb1d8b6f70c0f9767d586ea2a2ca6f1
BLAKE2b-256 checksum
How to use checksums
2f93bb42d3c99bec82878089ba67bf5a0505aea51931b3d7d7f01c33673c54da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.0 Darwin/23.1.0

Security for pydantic_mongo_orm 0.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page