问题
I have problem to save data from QuerySelectField with query_factory. I will always end with:
Error message
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>)]
DB Model
# --- 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
Form
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')
Route
@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')
...
It seems that I can't retrieve the actual data from the query_factory function (asset_owner)?
Appreciate your assistance/help!
回答1:
The problem is that you're providing an instance of Model_New_Users
as the 2nd (0-indexed) parameter.
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>)]
But in your model you've defined it as a String
type:
asset_owner = db.Column(db.String(64), unique=False, index=True)
This is queried via a lambda in your form:
asset_owner = QuerySelectField(u'Owner',
query_factory=lambda: Model_New_Users.query.all(),
get_label='owner_name')
And then the entire object is passed in your update. I think this is the bug.
add_data = Model_New_Asset(asset_name=form.asset_name.data,
asset_type=form.asset_type.data,
asset_owner=form.asset_owner.data)
I think you can fix it by specifying which field on the Model_New_Users
you intend to pass. Something like form.asset_owner.data.owner_name
or whatever.
回答2:
The reason you get an instance back is because you haven't given a
def __str__():
representation. Hope this help's newbies like me.
来源:https://stackoverflow.com/questions/41396127/get-data-values-from-queryselectfield-with-query-factory