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-1.0.0.tar.gz
(7.2 kB
view hashes)
Built Distribution
Close
Hashes for pydantic_mongo-1.0.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b53a851b39302b015438bd277a3548889707b75daeae0c452bd605d7a4686bcc |
|
MD5 | 5dc1ed62c20caa98dfadf1d14daac919 |
|
BLAKE2b-256 | d3f5bc26594147599d663c4ed3dcf12a60a6cd39bd63deb7e5f562449ed366ee |