使用SQLAlchemy将QuerySelectField选择存储在数据库中

时间:2019-06-22 16:48:52

标签: python sqlalchemy flask-sqlalchemy flask-wtforms

我正在将QuerySelectField与SQLAlchemy和WTForms一起使用,以从数据库中选择一个选项。我想选择该选项,然后将该选项或对该选项的引用存储在新列中,但出现错误。

sqlalchemy.exc.InterfaceError:(sqlite3.InterfaceError)错误绑定参数1-可能是不受支持的类型。 [SQL:INSERT INTO选择(risk_level,opts,urls_affected)值(?,?,?)] [参数:('low','aaa')](此错误的背景:http://sqlalche.me/e/rvf5)< / p>

我认为错误是因为我错过了在选择我的选项并将其添加到数据库之间的步骤,而该选择不是数据库模型指定的字符串?


def choose_finding():
    return Finding.query

class SelectForm(FlaskForm):
    risk_level = SelectField('Risk Level',choices=[('low','Low Risk'), ('medium','Medium Risk'),
                            ('high','High Risk'), ('severe','Severe Risk')],validators=[DataRequired()])
    urls_affected = TextField('URLs Affected',validators=[DataRequired()], widget=TextArea())
    opts = QuerySelectField('Choose Finding',query_factory=choose_finding, get_label='finding_name')
    submit = SubmitField('Submit')
class Choice(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    risk_level = db.Column(db.String(40), nullable=False)
    opts = db.Column(db.String(250), nullable=False)
    urls_affected = db.Column(db.Text, nullable=False)

@app.route("/select_findings", methods=['GET','POST'])
def select_findings():
        form = SelectForm()
        if form.validate_on_submit():
            finding_choice=Choice(risk_level=form.risk_level.data,
            opts=form.opts.data, urls_affected=form.urls_affected.data)
            db.session.add(finding_choice)
            db.session.commit()
            flash('Finding added to report','success')
            return redirect(url_for('select_findings'))
        return render_template('select_findings.html', title='Select Findings', form=form)

0 个答案:

没有答案