2014-04-22 99 views
4

將兩個錶鏈接在一起,並試圖創建一個外鍵的表單。錯誤消息:InterfaceError:<unprintable InterfaceError object>

sqlalchemy.exc.InterfaceError

InterfaceError:不可打印InterfaceError對象

datbase鏈接來實現的:

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 

我不認爲我寫的功能很對,有意見/幫助嗎?

+0

你使用的SQL引擎? – silpol

回答

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) 
    ... 
+0

你使用哪種SQL引擎? – silpol

+0

@ Jeremy Nikolai:我遇到同樣的情況,我也使用'QuerySelectField',請你看看[this](http://stackoverflow.com/questions/43908969/flask-admin-sqlalchemy-exc -interfaceerrorerror-binding-parameter-8),謝謝。 – Samoth