错误信息:接口错误:<不可打印的接口错误对象>
我正在把两个表连接在一起,并试图创建一个包含外键的表单。
出现了一个错误:sqlalchemy.exc.InterfaceError
错误信息是:无法打印的 InterfaceError 对象
数据库连接的代码如下:
class Client(db.Model):
__tablename__ = 'Client'
..
stand_id = db.Column(db.String(10), index = True, unique = True)
stands = db.relationship('Stand', backref= 'Stand', lazy='select')
def __init__(self,client_name,contact_number,contact_name,contact_email,stand_id):
self.client_name = client_name
self.contact_number = contact_number
self.contact_email = contact_email
self.contact_name = contact_name
self.stand_id = stand_id
class Stand(db.Model):
__tablename__ = 'Stand'
..
stand_number = db.Column(db.String(10), db.ForeignKey('Client.stand_id' ))
def __repr__(self):
return '<Stand %r>' % (self.stand_id)
def __init__(self, stand_name,items, quantity,install_date,
derig_date,comments,last_update, stand_number):
self.stand_name = stand_name
self.items = items
self.quantity = quantity
self.install_date = install_date
self.derig_date = derig_date
self.comments = comments
self.last_update = last_update
self.stand_number = stand_number
表单的代码如下:
class StandForm(Form):
stand_name = TextField('stand_name', validators = [Required()])
items = TextAreaField('items', validators = [Required()])
quantity = TextAreaField('quantity', validators = [Required()])
install_date = TextField('install_date',validators = [Required()])
derig_date = TextField('derig_date', validators = [Required()])
comments = TextField('comments', validators = [Required()])
last_update = TextField('last_update', validators = [Required()])
stand_number = QuerySelectField(query_factory=lambda: Client.query.all())
视图的代码如下:
@app.route('/newstand', methods = ['GET','POST'])
def newstand():
form = StandForm()
if form.validate():
stand = Stand(
request.form['stand_name'], request.form['items'], request.form['quantity'],
request.form['install_date'],request.form['derig_date'], request.form['comments'],
request.form['last_update'], request.form['stand_number'])
form.populate_obj(stand)
db.session.add(stand)
db.session.commit()
return render_template('liststands.html',
stand = stand,
form=form)
else:
flash("Your form contained errors")
return render_template('newstand.html', form = form
我觉得我写的函数可能有点问题,有什么意见或帮助吗?
1 个回答
2
我刚遇到同样的错误,看起来可能是类似的问题。QuerySelectField
返回的是完整的对象,而不仅仅是 ID。对于上面的例子:
把表单字段的名字改成能反映它包含内容的名字:
class StandForm(Form):
...
stand = QuerySelectField(query_factory=lambda: Client.query.all())
在你的视图中构建对象时,记得使用 ID:
@app.route('/newstand', methods = ['GET','POST'])
def newstand():
form = StandForm()
if form.validate():
stand = Stand(
request.form['stand_name'], request.form['items'], request.form['quantity'],
request.form['install_date'],request.form['derig_date'], request.form['comments'],
request.form['last_update'], request.form['stand'].stand_id)
...
另外,上面的例子中同时用表单内容明确构建了 Stand
对象,还调用了 form.populate_obj
,这可能是多余的(因为它会再次填充对象的所有相同字段),所以其中一个可以去掉。
如果 form.populate_obj
能自动处理外键(我想应该是这样,虽然我没试过),那么你可以把视图代码简化成:
@app.route('/newstand', methods = ['GET','POST'])
def newstand():
form = StandForm()
if form.validate():
stand = Stand()
form.populate_obj(stand)
...