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.2.tar.gz
(7.1 kB
view hashes)
Built Distribution
Close
Hashes for pydantic_mongo-0.1.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5865c231258b9e8ef724fa2fb45d30d5857c72e29d6ae0170b8bf090ab3081c |
|
MD5 | 3523aa5a927f085e0d98c847f359e498 |
|
BLAKE2b-256 | 2cede6891b4e55f6347905736983ebfdfb196a8db11af69046ccb615e9bb4499 |