Document object mapper for pydantic and pymongo
Project description
Document object mapper for pydantic and pymongo
Documentation
Usage
Install:
$ pip install pydantic-mongo
from pydantic import BaseModel
from pydantic_mongo import AbstractRepository, ObjectIdField
from pymongo import MongoClient
class Foo(BaseModel):
count: int
size: float = None
class Bar(BaseModel):
apple = 'x'
banana = 'y'
class Spam(BaseModel):
id: ObjectIdField = None
foo: Foo
bars: List[Bar]
class Config:
# The ObjectIdField creates an bson ObjectId value, so its necessary to setup the json encoding
json_encoders = {ObjectId: str}
class SpamRepository(AbstractRepository[Spam]):
class Meta:
collection_name = 'spams'
client = MongoClient(os.environ["MONGODB_URL"])
database = client[os.environ["MONGODB_DATABASE"]]
spam = Spam(foo=Foo(count=1, size=1.0),bars=[Bar()])
spam_repository = SpamRepository(database=database)
# Insert / Update
spam_repository.save(spam)
# Delete
spam_repository.delete(spam)
# Find One By Id
result = spam_repository.find_one_by_id(spam.id)
# Find One By Query
result = spam_repository.find_one_by({'foo.count': 1})
# Find By Query
results = spam_repository.find_by({'foo.count': {'$gte': 1}})
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
Close
Hashes for pydantic-mongo-0.1.0.linux-x86_64.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | efb21693c81d798513efa5d4f9cbb40ab7109d1f09a03c4f272634fcdb5b390a |
|
MD5 | 9ab09916cc40a6efaf1f9a3e98b42704 |
|
BLAKE2b-256 | 1fd700985848f40720552e6d72e13180ed6221a7d9238248d0f7199c6319ba4e |
Close
Hashes for pydantic_mongo-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0af93d91e32fb117ed82711b0cc1c97cfbefb315aa50528f774f973c3d463d56 |
|
MD5 | 90ed4530d2105555097577bdb56f7b5a |
|
BLAKE2b-256 | 13415cf9f1afc33164511eef420661b4978cde9ff7c8aefb27ac50e9b00c574e |