Package: pydantic_mongo_orm
Pydantic Mongo ORMPydantic Mongo ORM is a library that allows users to map Pydantic models to MongoDb.Installationpip install pydantic_mongo_ormUsageModelsPMO uses Pydantic models to represent data in database. To start using it, you need to create new model based onpydantic_mongo_orm.BaseModel. This class implements all the features from Pydantic but also adds some
orm-specific functionality, e.g. adds read-only fieldidthat represents document-id (_id) from MongoDb:frompydantic_mongo_ormimportBaseModelclassMyModel(BaseModel):greeting:strmy_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 specifymongo_configfor each model class that further modifies ORM behaviour:frompydantic_mongo_ormimportBaseModel,MongoConfigDictclassMyModel(BaseModel):greeting:strmongo_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,ObjectsStorageis
used. Explained laterStorageThe base of PMO isStorage- 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:frompydantic_mongo_ormimportStoragestorage=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 objectsYou may useStorageobject directly but PMO offers a shorthand that simplifies working with the ORM. Each
model class provides attributeobjectsthat provides direct access to collection storing that particular model.
To enable this feature, you must first bind theStorageto theBaseModelusing.bind(). This example is
equivalent with the previous one:frompydantic_mongo_ormimportStoragestorage=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, theobjectsattribute contains an instance ofpydantic_mongo_orm.ObjectsStorage. It provides the
same functionality aspydantic_mongo_orm.Storage. If you wish to customize behaviour of certain model - change
how it's stored, add custom filters etc., you may extend theObjectsStorageclass, add new functionality and
then configure it to be used for your models:frompydantic_mongo_ormimportBaseModel,ObjectsStorage,Storage,MongoConfigDictclassRectangleStorage(ObjectsStorage):defsquares(self):returnself.find({'$where':'this.x == this.y'})classRectangle(BaseModel):mongo_config=MongoConfigDict(objects_class=RectangleStorage)length:intwidth:intstorage=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]