wtf-forms populate.object AttributeError:编辑数据库条目时,“加热电路”对象没有属性“翻译”

时间:2019-04-26 21:28:11

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

首先,我知道问题是什么,但是我不知道为什么或如何解决它,我知道该函数需要将字符串传递给数据库,我在其中有一个用于创建和编辑的视图函数。创建部件我已经在功能中添加了“ str”以解决问题,但是我不知道如何为编辑部件执行此操作。表单具有一个为创建部件正确填充的querySelectField,当我提交“ str”时,将正确转换查询结果。

因此,为了解决该问题,我在函数的创建部分添加了“ str”,但是我看不到如何使用“ populate.obj”将其添加到编辑部分,我也希望该字段为创建表单时,用存储在数据库中的数据填充,但不是。只是此QuerySelectfield引起了问题。这是我的一些代码。

@bp.route('/control/addheatingcircuit', methods=['GET', 'POST']) 
@bp.route('/control/addheatingcircuit/<int:id>', methods=('GET', 
'POST')) 
@login_required
def addheatingcircuit(id=None):
    if id is not None:
        obj = Heatingcircuit.query.get(id) or Heatingcircuit()
        form = AddHeatingcircuitForm(request.form, obj=obj)
        if form.validate_on_submit():
            form.populate_obj(obj)
            db.session.add(obj)
            db.session.commit()
            return redirect(url_for('index.index'))
    else:
        form = AddHeatingcircuitForm()
        if form.validate_on_submit():
            heatingcircuit = Heatingcircuit(name=form.name.data, 
sensor_ID1=str(form.sensor_ID1.data),\ #here i have added 'str'
                           pin_ID1=str(form.pin_ID1.data))
            db.session.add(heatingcircuit)
            db.session.commit()
            flash('Congratulations, you are now a registered a new 
device!')
            return redirect(url_for('index.index'))
    return render_template('control/addheatingcircuit.html', title= 
'add edit heating circuit',
                          form=form)

forms.py

from wtforms_alchemy.fields import QuerySelectField
class AddHeatingcircuitForm(FlaskForm):
name = StringField('Name', validators=[DataRequired()])
sensor_ID1 = QuerySelectField('sensor_ID1', 
                query_factory=lambda: Sensors.query.all(),
                allow_blank=True, get_label='sensorID')
sensor_ID2 = QuerySelectField('sensor_ID2', 
                query_factory=lambda: Sensors.query.all(),
                allow_blank=True, get_label='sensorID')
.....

models.py

class Heatingcircuit(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64))
    sensor_ID1 = db.Column(db.String(30))
    sensor_ID2 = db.Column(db.String(30))
    sensor_ID3 = db.Column(db.String(30))
    pin_ID1 = db.Column(db.Integer)
    pin_ID2 = db.Column(db.Integer)
    pin_ID3 = db.Column(db.Integer)
    temp = db.Column(db.Float)
    state = db.Column(db.String(9))
    timer = db.relationship('Timers', backref='time')

    def __repr__(self):
        return '{}'.format(self.id)

我在论坛上看了很多问题,但这是最接近的匹配Flask WTF 'StringField' object has no attribute 'translate'

因此,正如我所解释的,我期望编辑现有条目部分上的功能将使用已经注册的信息填充表单,并且当我更改此表单并提交填充的表单时,该函数会被排除;相反,我会收到此错误消息;

AttributeError: 'Heatingcircuit' object has no attribute 'translate'

(如果需要,可以提供完整的错误日志)

非常感谢Paul

编辑,我正在看theis教程https://www.rithmschool.com/courses/flask-fundamentals/forms-with-wtforms 这里讨论CSRF,以及如何禁用令牌以遍历表单,请问我的问题是吗? 这个问题也会影响SelectField。

Edit2 我通过错误“ sqlalchemy.exc.InvalidRequestError:Entity”没有属性“ enabled””尝试了链接http://wtforms.simplecodes.com/docs/0.6.1/ext.html#module-wtforms.ext.sqlalchemy.fields中提到的想法 这是我的代码

def enabled_sensors():
    return Sensors.query.filter_by(enabled=True)

但这无法 编辑错误

Traceback (most recent call last):
  File "/home/pi/heating/venv/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
  File "/home/pi/heating/venv/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
  File "/home/pi/heating/venv/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
 File "/home/pi/heating/venv/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
  File "/home/pi/heating/venv/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/flask_login/utils.py", line 261, in decorated_view
return func(*args, **kwargs)
  File "/home/pi/heating/homeHeating/control/control.py", line 154, in addheatingcircuit
db.session.commit()
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/orm/scoping.py", line 162, in do
return getattr(self.registry(), name)(*args, **kwargs)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1026, in commit
self.transaction.commit()
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 493, in commit
self._prepare_impl()
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 472, in _prepare_impl
self.session.flush()
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2451, in flush
self._flush(objects)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2589, in _flush
transaction.rollback(_capture_exception=True)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 129, in reraise
raise value
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2549, in _flush
flush_context.execute()
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
rec.execute(self)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute
uow,
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 236, in save_obj
update,
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 978, in _emit_update_statements
statement, multiparams
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 988, in execute
return meth(self, multiparams, params)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 287, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1107, in _execute_clauseelement
distilled_params,
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1248, in _execute_context
e, statement, parameters, cursor, context
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1468, in _handle_dbapi_exception
util.reraise(*exc_info)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 129, in reraise
raise value
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1244, in _execute_context
cursor, statement, parameters, context
  File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 552, in do_execute
cursor.execute(statement, parameters)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/pymysql/cursors.py", line 168, in execute
query = self.mogrify(query, args)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/pymysql/cursors.py", line 147, in mogrify
query = query % self._escape_args(args, conn)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/pymysql/cursors.py", line 127, in _escape_args
return {key: conn.literal(val) for (key, val) in args.items()}
  File "/home/pi/heating/venv/lib/python3.7/site-packages/pymysql/cursors.py", line 127, in <dictcomp>
return {key: conn.literal(val) for (key, val) in args.items()}
  File "/home/pi/heating/venv/lib/python3.7/site-packages/pymysql/connections.py", line 467, in literal
return self.escape(obj, self.encoders)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/pymysql/connections.py", line 460, in escape
return converters.escape_item(obj, self.charset, mapping=mapping)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/pymysql/converters.py", line 27, in escape_item
val = encoder(val, mapping)
 File "/home/pi/heating/venv/lib/python3.7/site-packages/pymysql/converters.py", line 118, in escape_unicode
return u"'%s'" % _escape_unicode(value)
  File "/home/pi/heating/venv/lib/python3.7/site-packages/pymysql/converters.py", line 73, in _escape_unicode
    return value.translate(_escape_table)
AttributeError: 'Sensors' object has no attribute 'translate'

对于“ def addpin():”函数的后半部分,我也有同样的错误消息,我重新进行了搜索,发现我需要返回一个字符串,因此我添加了str(form.sensor_ID1.data)“ str”,这解决了那里的问题,但是功能的前半部分是编辑部分,现在我遇到了问题,但不知道如何解决。 我相信错误在这里;

    if id is not None:
        obj = Heatingcircuit.query.get(id) or Heatingcircuit()
        form = AddHeatingcircuitForm(request.form, obj=obj)
        if form.validate_on_submit():
            form.populate_obj(obj)
            db.session.add(obj)
            db.session.commit()

感谢您的帮助 温暖的问候 保罗

1 个答案:

答案 0 :(得分:0)

两个问题。首先,QuerySelectFieldquery_factory参数接受查询,而不是结果列表。因此,请卸下.all()

sensor_ID1 = QuerySelectField('sensor_ID1', 
                query_factory=lambda: Sensors.query,
                allow_blank=True, get_label='sensorID')

第二,给定表单字段的data将为您提供Sensors对象。因此,您不能仅仅投射它,还需要询问它的idpin_ID1字段也是如此。如何执行将取决于如何声明Sensors类:

heatingcircuit = Heatingcircuit(
    name=form.name.data,
    sensor_ID1=form.sensor_ID1.data.sensorID,
    pin_ID1=form.pin_ID1.data.pinID
)