当前位置:  开发笔记 > 编程语言 > 正文

python wtf AttributeError:'ObjectIdField'对象没有属性'help_text'

如何解决《pythonwtfAttributeError:'ObjectIdField'对象没有属性'help_text'》经验,为你挑选了1个好方法。

基于本教程,我正在尝试创建一个表单来进行一些测量.似乎显示数据的部分正在工作,但是当使用model_form命令生成输入表单时,它会突然出现以下错误:

在转换'description'中输入文件"/myproject/lib/python3.4/site-packages/flask_mongoengine/wtf/orm.py",第49行: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('//', 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属性相关的任何内容.任何人都能理解那里有什么问题吗?



1> Ville Vaara..:

编辑:

根据用户Jérôme的评论#2,烧瓶 - mongoengine(和mongoengine 0.10.5)的当前版本(在此编辑时为0.7.5)直接开箱即用.经过测试和验证.

我正在做相同的教程,也遇到了这个错误.

help_text="Your helptext here."在models.py中添加了字段(这些是StringFields)并且解决了这个问题,但是你的错误指向了ObjectIdField,这让它变得更加令人费解.

所以这并不是非常有用,但也许它会激励你朝着正确的方向前进.

编辑:

我对此进行了一段时间的挣扎,结果发现mongoengine已经发生了变化,导致了烧瓶_Mongoengine的损坏. 这个在flask_mongoengine中的提交反过来修复了这个问题.

至于实际的解决方案,使用以前版本的mongoengine(0.10.1)为我工作.所以:

$ pip uninstall mongoengine
$ pip install mongoengine==0.10.1

推荐阅读
郑小蒜9299_941611_G
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有