基于this教程,我正在尝试创建一个表单来进行一些测量。似乎显示数据的部分正在工作,但是当使用model_form
命令生成输入表单时,它会出现此错误:
文件“/myproject/lib/python3.4/site-packages/flask_mongoengine/wtf/orm.py”,第49行,转换 'description':field.help_text或'',
AttributeError:'ObjectIdField'对象没有属性'help_text'
错误发生在我的代码的这一行:
form_cls = model_form(Measurement, exclude=('id', 'created_at', 'comments'))
这是我的view.py代码:
from flask import Blueprint, request, redirect, render_template, url_for
from flask.views import MethodView
from flask.ext.mongoengine.wtf import model_form
from pyReefLog.models import Measurement
measurements = Blueprint('measurements', __name__, template_folder='templates')
class List(MethodView):
cls = Measurement
def get(self):
measurements = self.cls.objects.all()
return render_template('measurements/list.html', measurements=measurements)
class Detail(MethodView):
def get_context(self, creator=None):
form_cls = model_form(Measurement, exclude=('id', 'created_at', 'comments'))
if creator:
measurement = Measurement.objects.get_or_404(creator=creator)
if request.method == 'POST':
form = form_cls(request.form, inital=measurement._data)
else:
form = form_cls(obj=measurement)
else:
measurement = Measurement()
form = form_cls(request.form)
context = {
"measurement": measurement,
"form": form,
"create": creator is None
}
return context
def get(self, creator):
context = self.get_context(creator)
return render_template('measurements/detail.html', **context)
def post(self, creator):
context = self.get_context(creator)
form = context.get('form')
if form.validate():
post = context.get('post')
form.populate_obj(post)
post.save()
return redirect(url_for('measurements.index'))
return render_template('measurements/detail.html', **context)
# Register the urls
measurements.add_url_rule('/', view_func=List.as_view('index'))
measurements.add_url_rule('/create/', defaults={'creator': None}, view_func=Detail.as_view('create'))
measurements.add_url_rule('/<creator>/', view_func=Detail.as_view('edit'))
这是我的models.py:
import datetime
from flask import url_for
from pyReefLog import db
class Comment(db.EmbeddedDocument):
created_at = db.DateTimeField(default=datetime.datetime.now, required=True)
body = db.StringField(verbose_name="Comment", required=True)
author = db.StringField(verbose_name="Name", max_length=255, required=True)
class Measurement(db.Document):
created_at = db.DateTimeField(default=datetime.datetime.now, required=True)
measured_at = db.DateTimeField(default=datetime.datetime.now, required=True)
alkalinity = db.DecimalField(default=0.0, precision=2)
calcium = db.DecimalField(default=0.0, precision=2)
magnesium = db.DecimalField(default=0.0, precision=2)
ph = db.DecimalField(default=0.0, precision=2)
phosphate = db.DecimalField(default=0.0, precision=2)
salinity = db.DecimalField(default=0.0, precision=2)
temperature = db.DecimalField(default=0.0, precision=2)
creator = db.StringField(required=True, max_length=255)
comments = db.ListField(db.EmbeddedDocumentField('Comment'))
def get_absolute_url(self):
return url_for('measurement', kwargs={"creator": self.creator})
def __unicode__(self):
return self.creator
meta = {
'allow_inheritance': True,
'indexes': ['-created_at', '-measured_at', 'creator'],
'ordering': ['-measured_at']
}
wtForms教程都没有提到与help_text属性相关的任何内容。 任何人都可以理解那里有什么问题吗?
答案 0 :(得分:5)
编辑:
根据用户Jérôme的评论#2,烧瓶 - mongoengine(和mongoengine 0.10.5)的当前版本(在此编辑时为0.7.5)直接开箱即用。经过测试和验证。
我正在做相同的教程并遇到了这个错误。
我在models.py中的字段(这些是StringFields)中添加了help_text="Your helptext here."
并解决了这个问题,但是您的错误指向了ObjectIdField,这让它更加令人费解。
所以这不是非常有用,但也许它会激励你朝着正确的方向前进。
编辑:
我对此进行了一段时间的努力,结果发现mongoengine的变化打破了烧瓶_Mongoengine。 This commit in flask_mongoengine in turn fixes that.
至于实际解决方案,使用以前版本的mongoengine(0.10.1)对我有用。所以:
$ pip uninstall mongoengine
$ pip install mongoengine==0.10.1
答案 1 :(得分:1)
Flask-MongoEngine 0.7.5
的当前可用版本解决了此问题。
它解决了MongoEngine已弃用的current_profile
和help_text
属性问题。
使用safe
升级现有版本的Flask-MongoEngine
。