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

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.

主站蜘蛛池模板: 阜康市| 屏东市| 西青区| 苏尼特右旗| 河南省| 梓潼县| 龙山县| 吉木乃县| 大英县| 临泽县| 溧阳市| 射洪县| 抚州市| 喀喇沁旗| 乐安县| 五华县| 灵璧县| 新干县| 同德县| 山丹县| 西充县| 镶黄旗| 启东市| 开原市| 龙南县| 漯河市| 磴口县| 荃湾区| 吴川市| 南溪县| 怀安县| 思茅市| 类乌齐县| 洮南市| 垫江县| 阳高县| 新安县| 札达县| 武胜县| 申扎县| 周口市|