官术网_书友最值得收藏!

PyMODM ODM

Similar to Ruby's Mongoid, PyMODM is an ODM for Python that follows closely on Django's built-in ORM. Installing it can be done via pip:

pip install pymodm

Then we need to edit settings.py and replace the database engine with a dummy database:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.dummy'
}
}

And add our connection string anywhere in settings.py:

from pymodm import connect
connect("mongodb://localhost:27017/myDatabase", alias="MyApplication")

Here we have to use a connection string that has the following structure:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

Options have to be pairs of name=value with an & between each pair. Some interesting pairs are:

Model classes need to inherit from MongoModel. A sample class will look like this:

from pymodm import MongoModel, fields
class User(MongoModel):
email = fields.EmailField(primary_key=True)
first_name = fields.CharField()
last_name = fields.CharField()

This has a User class with first_name, last_name, and email fields where email is the primary field.

主站蜘蛛池模板: 永昌县| 博客| 武城县| 湘阴县| 仁化县| 郸城县| 宜城市| 伊金霍洛旗| 微山县| 东至县| 郎溪县| 普定县| 潜江市| 东乌珠穆沁旗| 农安县| 获嘉县| 洪洞县| 攀枝花市| 咸阳市| 瓦房店市| 宜川县| 建始县| 平昌县| 徐州市| 松原市| 宜丰县| 拉孜县| 西畴县| 庆阳市| 中宁县| 桦川县| 临沂市| 泾川县| 河间市| 闽侯县| 克山县| 长兴县| 古田县| 建始县| 贺州市| 临清市|