python wtf AttributeError: 'ObjectIdField' object has no attribute 'help_text'

安稳与你 提交于 2019-12-12 09:14:55

问题


Based on this tutorial I am trying to create a form to get a few measurements. It seems that the part to display the data is working but when using the model_form command to generate the input form it breaks with this error:

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'

The error happens on this line of my code:

form_cls = model_form(Measurement, exclude=('id', 'created_at', 'comments'))

This is my view.py code:

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'))

And this is my 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']
    }

None of the wtForms tutorial mention anything related to help_text attribute. Can anyone understand what is wrong there?


回答1:


Edit:

As per comment #2 by user Jérôme, the current version (0.7.5 at the moment of this edit) of flask-mongoengine (and mongoengine 0.10.5) works straight out of the box. Tested and verified.

I'm doing the same tutorial and encountered that error too.

I added help_text="Your helptext here." to the fields (these were StringFields) in models.py and that solved the issue, but your error points to ObjectIdField, and that makes it a lot more puzzling.

So this isn't horribly helpful, but maybe it'll inspire you in the right direction.

Edit:

I wrestled with this for a while more, and turns out there's been a change in mongoengine that broke flask_mongoengine. This commit in flask_mongoengine in turn fixes that.

As for actual solutions, using a previous version of mongoengine (0.10.1) worked for me. So:

$ pip uninstall mongoengine
$ pip install mongoengine==0.10.1



回答2:


Current available version of Flask-MongoEngine 0.7.5 fixes this issue.

It resolves MongoEngine deprecated help_text and safe attribute issues.

Upgrade your existing version of Flask-MongoEngine using pip install --upgrade flask-mongoengine.



来源:https://stackoverflow.com/questions/34161163/python-wtf-attributeerror-objectidfield-object-has-no-attribute-help-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!