gpt4 book ai didi

python - 错误信息 : InterfaceError:

转载 作者:太空宇宙 更新时间:2023-11-03 11:30:01 24 4
gpt4 key购买 nike

将两个表链接在一起并尝试创建一个包含外键的表单。

sqlalchemy.exc.InterfaceError

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

我觉得我写的函数不太对,有什么意见/帮助吗?

最佳答案

我刚刚遇到了同样的错误,看起来它可能是一个类似的问题。QuerySelectField 返回完整的对象,而不仅仅是 ID。对于上面的例子:

重命名表单域以反射(reflect)它包含的内容:

class StandForm(Form):
...
stand = QuerySelectField(query_factory=lambda: Client.query.all())

并在您的 View 中构造对象时使用 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 自动尊重外键(我假设它确实如此,虽然我没有尝试过),那么你可以将你的 View 代码简化为:

@app.route('/newstand', methods = ['GET','POST'])
def newstand():
form = StandForm()
if form.validate():
stand = Stand()
form.populate_obj(stand)
...

关于python - 错误信息 : InterfaceError: <unprintable InterfaceError object>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23208641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com