python wtf AttributeError: 'ObjectIdField' object has no attribute 'help_text'
python wtf AttributeError: 'ObjectIdField' object has no attribute 'help_text'
根据 this 教程,我正在尝试创建一个表格来进行一些测量。显示数据的部分似乎正在工作,但是当使用 model_form
命令生成输入表单时,它会因以下错误而中断:
File "/myproject/lib/python3.4/site-packages/flask_mongoengine/wtf/orm.py", line 49, in convert
'description': field.help_text or '',
AttributeError: 'ObjectIdField' object has no attribute '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 教程的 None 提到与 help_text 属性相关的任何内容。
谁能明白那里出了什么问题?
编辑:
根据用户 Jérôme 的评论 #2,flask-mongoengine(和 mongoengine 0.10.5)的当前版本(本次编辑时为 0.7.5)开箱即用。经过测试和验证。
我正在做同样的教程,也遇到了那个错误。
我在 models.py 中的字段(这些是 StringFields)中添加了 help_text="Your helptext here."
并解决了问题,但是您的错误指向 ObjectIdField,这让它变得更加令人费解。
所以这并不是非常有用,但也许它会在正确的方向上激励你。
编辑:
我为此苦苦思索了一段时间,结果发现 mongoengine 中的一个更改破坏了 flask_mongoengine。 This commit in flask_mongoengine in turn fixes that.
至于实际解决方案,使用以前版本的 mongoengine (0.10.1) 对我有用。所以:
$ pip uninstall mongoengine
$ pip install mongoengine==0.10.1
Flask-MongoEngine 0.7.5
的当前可用版本修复了此问题。
它解决了 MongoEngine 弃用的 help_text
和 safe
属性问题。
使用 pip install --upgrade flask-mongoengine
升级您现有的 Flask-MongoEngine
版本。
根据 this 教程,我正在尝试创建一个表格来进行一些测量。显示数据的部分似乎正在工作,但是当使用 model_form
命令生成输入表单时,它会因以下错误而中断:
File "/myproject/lib/python3.4/site-packages/flask_mongoengine/wtf/orm.py", line 49, in convert 'description': field.help_text or '',
AttributeError: 'ObjectIdField' object has no attribute '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 教程的 None 提到与 help_text 属性相关的任何内容。 谁能明白那里出了什么问题?
编辑:
根据用户 Jérôme 的评论 #2,flask-mongoengine(和 mongoengine 0.10.5)的当前版本(本次编辑时为 0.7.5)开箱即用。经过测试和验证。
我正在做同样的教程,也遇到了那个错误。
我在 models.py 中的字段(这些是 StringFields)中添加了 help_text="Your helptext here."
并解决了问题,但是您的错误指向 ObjectIdField,这让它变得更加令人费解。
所以这并不是非常有用,但也许它会在正确的方向上激励你。
编辑:
我为此苦苦思索了一段时间,结果发现 mongoengine 中的一个更改破坏了 flask_mongoengine。 This commit in flask_mongoengine in turn fixes that.
至于实际解决方案,使用以前版本的 mongoengine (0.10.1) 对我有用。所以:
$ pip uninstall mongoengine
$ pip install mongoengine==0.10.1
Flask-MongoEngine 0.7.5
的当前可用版本修复了此问题。
它解决了 MongoEngine 弃用的 help_text
和 safe
属性问题。
使用 pip install --upgrade flask-mongoengine
升级您现有的 Flask-MongoEngine
版本。