Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
wiki2:python:flask:flaskmongoengine [2015/08/08 14:14] alfred [Configuration] |
wiki2:python:flask:flaskmongoengine [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 45: | Línea 45: | ||
| </code> | </code> | ||
| + | ==== Model ==== | ||
| + | <code python> | ||
| + | from app import db | ||
| + | |||
| + | |||
| + | class Post(db.Document): | ||
| + | created_at = db.DateTimeField(default=datetime.datetime.now, required=True) | ||
| + | title = db.StringField(max_length=255, required=True) | ||
| + | slug = db.StringField(max_length=255, required=True) | ||
| + | body = db.StringField(required=True) | ||
| + | comments = db.ListField(db.EmbeddedDocumentField('Comment')) | ||
| + | |||
| + | def get_absolute_url(self): | ||
| + | return url_for('post', kwargs={"slug": self.slug}) | ||
| + | |||
| + | def __unicode__(self): | ||
| + | return self.title | ||
| + | |||
| + | meta = { | ||
| + | 'allow_inheritance': True, | ||
| + | 'indexes': ['-created_at', 'slug'], | ||
| + | 'ordering': ['-created_at'] | ||
| + | } | ||
| + | </code> | ||
| ==== Use ==== | ==== Use ==== | ||
| Flask MongoEngine attaches methods to MongoEngine: | Flask MongoEngine attaches methods to MongoEngine: | ||
| Línea 66: | Línea 90: | ||
| todo = Todo.objects.get_or_404(_id=todo_id) | todo = Todo.objects.get_or_404(_id=todo_id) | ||
| paginated_tags = todo.paginate_field('tags', page, per_page=10) | paginated_tags = todo.paginate_field('tags', page, per_page=10) | ||
| + | </code> | ||
| + | |||
| + | You can use them in templates: | ||
| + | <code> | ||
| + | {% 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 %} | ||
| + | </code> | ||
| + | |||
| + | ===== Other uses ===== | ||
| + | ==== Session storage ==== | ||
| + | To use MongoEngine as your session store simple configure the session interface: | ||
| + | <code python> | ||
| + | from flask.ext.mongoengine import MongoEngine, MongoEngineSessionInterface | ||
| + | app = Flask(__name__) | ||
| + | db = MongoEngine(app) | ||
| + | app.session_interface = MongoEngineSessionInterface(db) | ||
| + | </code> | ||
| + | |||
| + | ==== 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. | ||
| + | |||
| + | <code python> | ||
| + | from flask import Flask from flask_debugtoolbar import DebugToolbarExtension | ||
| + | app = Flask(__name__) | ||
| + | app.config[‘DEBUG_TB_PANELS’] = [‘flask.ext.mongoengine.panels.MongoDebugPanel’] | ||
| + | db = MongoEngine(app) | ||
| + | toolbar = DebugToolbarExtension(app) | ||
| </code> | </code> | ||