A Pythonic Object Data Manager
Project description
A database-agnostic Document-Object Mapper for Python.
Install
$ pip install modular-odm
Example Usage with MongoDB
Defining Models
from modularodm import StoredObject, fields from modularodm.validators import MinLengthValidator, MaxLengthValidator class User(StoredObject): _meta = {"optimistic": True} _id = fields.StringField(primary=True, index=True) username = fields.StringField(required=True) password = fields.StringField(required=True, validate=[MinLengthValidator(8)]) def __repr__(self): return "<User: {0}>".format(self.username) class Comment(StoredObject): _meta = {"optimistic": True} _id = fields.StringField(primary=True, index=True) text = fields.StringField(validate=MaxLengthValidator(500)) user = fields.ForeignField("User", backref="comments") def __repr__(self): return "<Comment: {0}>".format(self.text)
Setting the Storage Backend
from pymongo import MongoClient from modularodm import storage client = MongoClient() db = client['testdb'] User.set_storage(storage.MongoStorage(db, collection="user")) Comment.set_storage(storage.MongoStorage(db, collection="comment"))
Creating and Querying
>>> from modularodm.query.querydialect import DefaultQueryDialect as Q >>> u = User(username="unladenswallow", password="h0lygrai1") >>> u.save() >>> comment = Comment(text="And now for something completely different.", user=u) >>> comment2 = Comment(text="It's just a flesh wound.", user=u) >>> comment.save() True >>> comment2.save() True >>> u = User.find_one(Q("username", "eq", "unladenswallow")) >>> u.comment__comments [<Comment: And now for something completely different.>, <Comment: It's just a flesh wound.>] >>> c = Comment.find(Q("text", "startswith", "And now"))[0] >>> c.text 'And now for something completely different.'
For more information regarding querying syntax, please visit the related readthedocs page at <http://modular-odm.readthedocs.org/en/latest/query_syntax.html>.
Migrations
TODO
Full documentation coming soon.
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.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size modular_odm-0.3.0-py2.py3-none-any.whl (42.0 kB) | File type Wheel | Python version 2.7 | Upload date | Hashes View |
Filename, size modular-odm-0.3.0.tar.gz (30.7 kB) | File type Source | Python version None | Upload date | Hashes View |
Close
Hashes for modular_odm-0.3.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 624db71ff7056934d7028239b62e858ff20f9b90d2ae08d6da363c86054dc80e |
|
MD5 | 4f7b7eca543a6016b72950ce94d85fe7 |
|
BLAKE2-256 | 73d3f7a5946600a0923fea6a0a8251ba77efb8dcca5a69fb46a757f937b121dc |