繁体   English   中英

使用函数输入作为代码Python

[英]Use function input as code Python

我有这个功能(SQLAlchemy):

# table: Table to search (should be the class name)
# column: filter to use
# search: search term
#
# Returns a list [object] with the found entries in the database
# Uses the SQL LIKE statement

def search(table, column, search):
    results = dbsession.query(table).filter(column.like('%' + search + '%')).all()
    return results

因此,此搜索功能在“表”类中进行搜索,使用过滤器“列”并搜索“搜索”。 我现在遇到的问题是,如果我输入'column'的值(实际上不是字符串,而是一段代码),我总是会收到一个错误的名称不存在的错误:

  users =  search(User, fname, 'Jo')
NameError: name 'fname' is not defined

有人知道如何正确编码吗?

请改用users = search(User, User.fname, 'Jo')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM