Flask-MongoEngine

0
48407

这是一个用于整合MongoEngine的Flask扩展。更多的信息,你可以参照MongoEngine文档

安装Flask-MongoEngine

通过pip安装

pip install flask-mongoengine

配置

基础配置是很简单的,仅用于实现扩展:


或者,你要在app初始化之前设置你的数据库,那么需要注意应用工厂:


 

By default, Flask-MongoEngine assumes(承担) that the mongod instance(实例) is running on localhost on port 27017, and you wish to connect to the database named test.

If MongoDB is running elsewhere(在别处), you should provide the host and port settings in the ‘MONGODB_SETTINGS’ dictionary wih app.config.:


If the database requires authentication(证明), the username and password arguments should be provided ‘MONGODB_SETTINGS’ dictionary wih app.config.:


 

Uri style connections are also supported, just supply the uri as the host in the ‘MONGODB_SETTINGS’ dictionary with app.config. Note that database name from uri has priority(优先) over name.


Connection settings may also be provided individually(个别地) by prefixing(加前缀) the setting with ‘MONGODB_’ in the app.config.:


 

自定义Queryset
flask-mongoengine attaches(依附) the following methods to Mongoengine’s default QuerySet:

get_or_404: works like .get(), but calls abort(中止计划)(404) if the object DoesNotExist.
first_or_404: same as above, except for .first().
paginate: paginates(为…标页数) the QuerySet. Takes two arguments, page and per_page.
paginate_field: paginates a field from one document in the QuerySet. Arguments: field_name, doc_id, page, per_page.
Examples:


 

Properties of the pagination(标记页数) object include: iter_pages, next, prev, has_next, has_prev, next_num, prev_num.

In the template:


MongoEngine and WTForms
You can use MongoEngine and WTForms like so:

from flask.ext.mongoengine.wtf import model_form

class User(db.Document):
email = db.StringField(required=True)
first_name = db.StringField(max_length=50)
last_name = db.StringField(max_length=50)

class Content(db.EmbeddedDocument):
text = db.StringField()
lang = db.StringField(max_length=3)

class Post(db.Document):
title = db.StringField(max_length=120, required=True)
author = db.ReferenceField(User)
tags = db.ListField(db.StringField(max_length=30))
content = db.EmbeddedDocumentField(Content)

PostForm = model_form(Post)

def add_post(request):
form = PostForm(request.POST)
if request.method == ‘POST’ and form.validate():
# do something
redirect(‘done’)
return render_template(‘add_post.html’, form=form)
Supported fields
StringField
BinaryField
URLField
EmailField
IntField
FloatField
DecimalField
BooleanField
DateTimeField
ListField (using wtforms.fields.FieldList )
SortedListField (duplicate(复制) ListField)
EmbeddedDocumentField (using wtforms.fields.FormField and generating(产生的) inline(内联的) Form)
ReferenceField (using wtforms.fields.SelectFieldBase with options loaded from QuerySet or Document)
DictField
Not currently supported field types:
ObjectIdField
GeoLocationField
GenericReferenceField
Session Interface¶
To use MongoEngine as your session(会议) store simple configure(安装) the session interface(界面):

from flask.ext.mongoengine import MongoEngine, MongoEngineSessionInterface

app = Flask(__name__)
db = MongoEngine(app)
app.session_interface = MongoEngineSessionInterface(db)

留下一个答复

Please enter your comment!
Please enter your name here