我一直想弄清楚如何將request.args傳遞給sqlalchemy過濾器。燒瓶 - 將request.args轉換爲字典
我想這應該工作:
model.query.filter(**request.args).all()
但它引發錯誤:
TypeError: <lambda>() got an unexpected keyword argument 'userid'
當用戶ID或任何其他GET ARG存在。
根據這篇文章 - https://stackoverflow.com/questions/19506105/flask-sqlalchemy-query-with-keyword-as-variable - 你可以傳遞一個字典到過濾器函數。
任何想法我做錯了什麼?
非常感謝:)
UPDATE:非常感謝下面的海報,但是現在它拋出以下錯誤:
ProgrammingError: (ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY tblclients.clientname' at line 3") 'SELECT favourites.id AS favourites_id, favourites.userid AS favourites_userid, favourites.clientid AS favourites_clientid, favourites.last_visit AS favourites_last_visit \nFROM favourites INNER JOIN tblclients ON tblclients.clientid = favourites.clientid \nWHERE favourites.userid = %s ORDER BY tblclients.clientname' ([u'41'],)
任何想法?
這是正確的答案。並且增加它:使用'request.args.to_dict()'是正確的,而'dict(request.args)'不是因爲它會產生一個字典,其值由列表組成。 – Kazanz