A super small package for validating and converting MongoDB ObjectId's to Pydantic models.
Project description
Pyd_Mon
An easy way to convert and validate Pymongo ObjectId's with Pydantic model fields.
Usage
Creating a MongoModel
Creating Pydantic models with a mapping id is as easy as inheriting from MongoModel and creating an id field with type of MongoId. All of your other fields can be created as normal pydantic fields.
from pyd_mon import MongoModel, MongoId
class Item(MongoModel):
id: MongoId # Will map to '_id'
name: str # The rest of the fields are standard Pydantic fields
Create Pydantic instance from Pymongo dict (_id -> id)
To map from a Pymongo dict you can use the from_mongo class method on your model.
result = collection.find_one() # Get item from Pymongo
item = Item.from_mongo(result) # Create a Pydantic instance from the Pymongo dict.
Create Pymongo dict from Pydantic instance (id -> _id)
To map back from a pydantic instance to a Pymongo dict call the mongo method on your model instance.
item = Item(id=MongoId(), name='Example')
collection.insert_one(item.mongo())
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyd-mon-0.2.0.tar.gz.
File metadata
- Download URL: pyd-mon-0.2.0.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.8 CPython/3.9.7 Linux/5.10.43.3-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c09ddefda003eb57b85aef49c2637d1c8e432b298074c073555513af58db376
|
|
| MD5 |
9397a762b6605ccead645273816f4026
|
|
| BLAKE2b-256 |
350829de16b8cf5756353ad8f8ce6f84756497c2623d307883437e859f6af3e7
|
File details
Details for the file pyd_mon-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pyd_mon-0.2.0-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.8 CPython/3.9.7 Linux/5.10.43.3-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a005207ab39bfdcc58f440adf3bcd73627a0e8d63b7e9ea0770273254af0dd1
|
|
| MD5 |
b7a519b45305c483910fa2c7b0db162d
|
|
| BLAKE2b-256 |
a85baca7392218bcf44d0a881376ab845e5a2d01866aa7e9b7ad74cbd82145f8
|