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
pydantic-mongo-0.1.1.tar.gz
(7.1 kB
view hashes)
Built Distribution
Close
Hashes for pydantic_mongo-0.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f3768922974c21e3ed4c14af6c4aa4305418cbe7e8b3a2bcfc83cf40b02cb7a |
|
MD5 | 3cb71bdd674f79afbac417262ffca286 |
|
BLAKE2b-256 | 81805be5e14e85563b885083cdd4547c4c9210ef1a2a1dab6e2b253e2571ef5a |