使用MongoEngine和Flask创建WTForm时的TypeError

时间:2014-08-25 17:35:32

标签: python-3.x flask mongoengine wtforms flask-mongoengine

我正在尝试使用Python3.4,Flask和MongoEngine创建一个简单的webapp。

尝试使用WTForms创建表单时遇到问题。

我有一个名为database的初始化MongoEngine应用程序:

database = MongoEngine(app)

我创建了一个名为Post的模型类:

class Post(database.Document):
    author = database.StringField(
        default='David Y. Stephenson', max_length=255, required=True
    )
    body = database.StringField(required=True)
    comments = database.ListField(
        database.EmbeddedDocumentField('Comment')
    )
    slug = database.StringField(max_length=255, required=True, unique=True)
    tease = database.StringField(max_length=255, required=True)
    time = database.DateTimeField(
        default=datetime.datetime.now, required=True
    )
    title = database.StringField(max_length=255, required=True, unique=True)

我有一个使用此类创建表单的路线:

@app.route('/blog/new')
def new():
    form = model_form(Post)
    return render_template('new_blog.html', form=form)

当我运行这条路线时,我得到一个TypeError:

TypeError: model must be a mongoengine Document schema

查看WTForm代码,似乎要求Post类是MongoEngine的BaseDocumentDocumentMetaclass classes的实例。 /usr/local/lib/python3.4/dist-packages/flask_mongoengine/wtf/orm.py的第223-224行读取:

from mongoengine.base import BaseDocument, DocumentMetaclass
if not isinstance(model, (BaseDocument, DocumentMetaclass)):
    raise TypeError('model must be a mongoengine Document schema'

当我尝试手动检查我的Post类时,它似乎是DocumentMetaclass,而不是BaseDocument。添加

app.logger.debug(isinstance(Post, mongoengine.base.BaseDocument))
app.logger.debug(isinstance(Post, mongoengine.base.DocumentMetaclass))

到我的代码返回:

--------------------------------------------------------------------------------
DEBUG in __init__ [__init__.py:57]:
False
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
DEBUG in __init__ [__init__.py:58]:
True
--------------------------------------------------------------------------------

我做错了什么?

我的__init__.py文件的全文是:

import datetime
from flask import Flask
from flask import render_template
from flask.ext.mongoengine import MongoEngine
from flask.ext.mongoengine.wtf import model_form
import mongoengine.base


app = Flask(__name__)


app.config['MONGODB_SETTINGS'] = {
    'db': 'davidystephenson',
    'host': 'ds059908.mongolab.com',
    'username': 'david',
    'password': 'opensecret',
    'port': 59908,
}
database = MongoEngine(app)


class Post(database.Document):
    author = database.StringField(
        default='David Y. Stephenson', max_length=255, required=True
    )
    body = database.StringField(required=True)
    comments = database.ListField(
        database.EmbeddedDocumentField('Comment')
    )
    slug = database.StringField(max_length=255, required=True, unique=True)
    tease = database.StringField(max_length=255, required=True)
    time = database.DateTimeField(
        default=datetime.datetime.now, required=True
    )
    title = database.StringField(max_length=255, required=True, unique=True)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/blog/')
def blog():
    posts = Post.objects.all()
    return render_template('blog.html', posts=posts)


@app.route('/blog/<slug>')
def post(slug):
    post = Post.objects.get(slug=slug)
    return render_template('post.html', post=post)


@app.route('/blog/new')
def new():
    app.logger.debug(isinstance(Post, mongoengine.base.BaseDocument))
    app.logger.debug(isinstance(Post, mongoengine.base.DocumentMetaclass))
    form = model_form(Post)
    return render_template('new_blog.html', form=form)

if __name__ == '__main__':
    app.run(debug=True)

2 个答案:

答案 0 :(得分:0)

您是否导入了初始化应用程序的数据库实例?

这是一个models.py可能是什么样子的例子。在我看来,当你写database.Document时,你的程序并不知道你的意思,所以我怀疑这是你的问题:

from app import db # <- Do you have this line in the same file as your class?

class Person(db.DynamicDocument):

    # Meta variables.
    meta = {
        'collection': 'people'
    }

# Document variables.
age = db.IntField()
name = db.StringField()

答案 1 :(得分:0)

我发现了同样的问题。

当我在异常之前打印所有局部变量时,我发现当前模型满足条件

if not isinstance(model, (BaseDocument, DocumentMetaclass)):

但接下来不会。下一个模型是相关模型的路径。在你的例子中是database.EmbeddedDocumentField(&#39; Comment&#39;)

使用列出的模型

的字符串路径定义列表属性ListField时引发的异常
comments = database.ListField(
    database.EmbeddedDocumentField('Comment')
)

而是用模型类

定义
comments = database.ListField(
    database.EmbeddedDocumentField(Comment)
)

我在Flask-Admin项目的pull-request中解决了这个bug。 https://github.com/mrjoes/flask-admin/pull/645