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:17] alfred [Use] |
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 99: | Línea 123: | ||
| ==== Debug Toolbar Panel ==== | ==== 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. | 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 | + | |
| + | <code python> | ||
| from flask import Flask from flask_debugtoolbar import DebugToolbarExtension | from flask import Flask from flask_debugtoolbar import DebugToolbarExtension | ||
| app = Flask(__name__) | app = Flask(__name__) | ||