pymorm 是一个基于 ORM框架的 PyMongo 接口。

代码示例:

from pymorm import MongoObject, MongoObjectMeta
from pymongo import MongoClIEnt

db = MongoClient("MongoDB://localhost:27017/pymorm").get_default_Database()


class Test(MongoObject):
    __metaclass__ = MongoObjectMeta
    __collection__ = db.tests

    __defaults__ = {"username": "Test",
                    "happiness": lambda: "poor"}

    def test_method(self, test):
        return test

    @property
    def test_property(self):
        return "test"

    @classmethod
    def test_classmethod(cls):
        return cls.__name__


user = Test.add({})
user2 = Test.add({"username": "Walter"})
user2.happiness = "a lot!"
print user
print user2
user.commit()

> User(_id=ObjectId('5519e5eb5dde7310f04d9bbe'), happiness=u'poor', username=u'Test') 
> User(_id=ObjectId('5519e5eb5dde7310f04d9bbe'), happiness=u'a lot!', username=u'Walter')

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务