factory从QuerySelectField获取数据值

factory从QuerySelectField获取数据值

本文介绍了使用query_factory从QuerySelectField获取数据值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用query_factory从QuerySelectField保存数据.我将始终以:

I have problem to save data from QuerySelectField with query_factory. I will always end with:

错误消息

InterfaceError: (sqlite3.InterfaceError)
Error binding parameter 2 - probably unsupported type.
[SQL: u'INSERT INTO asset_objects (asset_name, asset_type, asset_owner)
VALUES (?, ?, ?)'] [parameters: (u'd', u'Process', <__main__.Model_New_Users
object at 0x03819A50>)]

数据库模型

# --- New Information Assets ---
class Model_New_Asset(db.Model):
__tablename__ = 'asset_objects'
id = db.Column('asset_id', db.Integer, primary_key=True)
asset_name = db.Column(db.String(64), unique=False, index=True)
asset_type = db.Column(db.String(64), unique=False, index=True)
asset_owner = db.Column(db.String(64), unique=False, index=True)

def __init__(self, asset_name, asset_type, asset_owner):
    self.asset_name = asset_name
    self.asset_type = asset_type
    self.asset_owner = asset_owner

表格

class Form_New_Asset(Form):
    asset_name = StringField(u'Asset')
    asset_type = SelectField(u'Asset type',
                             choices=[('Process', 'Process'),
                                      ('Information', 'Information'),
                                      ('Informationssystem', 'Informationssystem'),
                                      ('Applikation', 'Applikation')])
    # From Model_New_Users
    asset_owner = QuerySelectField(u'Owner',
                                   query_factory=lambda: Model_New_Users.query.all(),
                                   get_label='owner_name')
    submit = SubmitField('Save')

路线

@app.route('/add_asset', methods=['GET', 'POST'])
def add_asset():
    form = Form_New_Asset()
    if request.method == 'POST':
        if not request.form['asset_name'] or not request.form['asset_type']:
            flash(u'Fill out all fields...', 'error')
        else:
            add_data = Model_New_Asset(asset_name=form.asset_name.data,
                                       asset_type=form.asset_type.data,
                                       asset_owner=form.asset_owner.data)
           db.session.add(add_data)
           db.session.commit()
           flash('Post saved', 'info')
 ...

似乎我无法从query_factory函数(asset_owner)中检索实际数据?

It seems that I can't retrieve the actual data from the query_factory function (asset_owner)?

感谢您的协助/帮助!

推荐答案

返回实例的原因是因为您没有给

The reason you get an instance back is because you haven't given a

def __str__():

表示.希望这个帮助像我这样的新手.

representation. Hope this help's newbies like me.

这篇关于使用query_factory从QuerySelectField获取数据值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 11:15