sqlalchemy:将表单输入传递给过滤器不会返回任何结果

时间:2017-11-14 22:09:32

标签: python sqlalchemy

db = create_engine('mysql://usr:passwd@localhost/DB', 
isolation_level="READ UNCOMMITTED")
metadata = MetaData(db)
Session = sessionmaker(bind=db)
session = Session()
server = Table('server', metadata, autoload=True)

if (model is not None):
  # The below == works and returns results
  #s = session.query(server).filter(server.c.product == model)
  #This returns nothing
  s = session.query(server).filter(server.c.product.like("%model%")).all()

模型作为表单参数传递

@app.route('/serverHandleList', methods=['POST'])
def serverHandleList():
model = request.form['inputModelName']
serverItems = getModel(model)

return render_template('some.html', items=serverItems, name=model)

我做错了什么?任何帮助非常感谢。谢谢!

1 个答案:

答案 0 :(得分:0)

您目前正在搜索“模型”这个词,而不是在变量model中传递的内容。

改为:

  s = session.query(server).filter(server.c.product.like("%{}%".format(model))).all()