Herramientas de usuario

Herramientas del sitio


wiki2:python:flask:flaskmongoengine

¡Esta es una revisión vieja del documento!


Flask MongoEngine

Basic usage

Install

pip install flask-mongoengine

Setting up the DB

from flask import Flask
from flask.ext.mongoengine import MongoEngine
 
app = Flask(__name__)
app.config.from_pyfile('the-config.cfg')
db = MongoEngine(app)

Or, if you are setting up your database before your app is initialized….

from flask import Flask
from flask.ext.mongoengine import MongoEngine
db = MongoEngine()
...
app = Flask(__name__)
app.config.from_pyfile('the-config.cfg')
db.init_app(app)

Configuration

app.config['MONGODB_SETTINGS'] = {
    'db': 'project1',
    'host': '192.168.1.35',
    'port': 12345,
    'username':'webapp',
    'password':'pwd123'
}
app.config['MONGODB_DB'] = 'project1'
app.config['MONGODB_HOST'] = '192.168.1.35'
app.config['MONGODB_PORT'] = 12345
app.config['MONGODB_USERNAME'] = 'webapp'
app.config['MONGODB_PASSWORD'] = 'pwd123'

Use

Flask MongoEngine attaches methods to MongoEngine:

  • 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.
# 404 if object doesn't exist
def view_todo(todo_id):
    todo = Todo.objects.get_or_404(_id=todo_id)
..
 
# Paginate through todo
def view_todos(page=1):
    paginated_todos = Todo.objects.paginate(page=page, per_page=10)
 
# Paginate through tags of todo
def view_todo_tags(todo_id, page=1):
    todo = Todo.objects.get_or_404(_id=todo_id)
    paginated_tags = todo.paginate_field('tags', page, per_page=10)

You can use them in templates:

{% macro render_pagination(pagination, endpoint) %}
  <div class=pagination>
  {%- for page in pagination.iter_pages() %}
    {% if page %}
      {% if page != pagination.page %}
        <a href="{{ url_for(endpoint, page=page) }}">{{ page }}</a>
      {% else %}
        <strong>{{ page }}</strong>
      {% endif %}
    {% else %}
      <span class=ellipsis>…</span>
    {% endif %}
  {%- endfor %}
  </div>
{% endmacro %}

Other uses

Session storage

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)

Debug Toolbar Panel

If you use the Flask-DebugToolbar you can add ‘flask.ext.mongoengine.panels.MongoDebugPanel’ to the DEBUG_TB_PANELS config list and then it will automatically track your queries.

from flask import Flask from flask_debugtoolbar import DebugToolbarExtension app = Flask(__name__) app.config db = MongoEngine(app) toolbar = DebugToolbarExtension(app)
 
wiki2/python/flask/flaskmongoengine.1439043474.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)