【发布时间】:2017-05-01 19:54:14
【问题描述】:
我正在尝试获取给定字符串address 在models.Listing.address 中的结果列表。我现在的代码是:
@app.route('/search/<address>')
def search(address):
results = models.Listing.select().where(address << models.Listing.address)
return render_template('search.html', results=results)
例如,我可能传入39 Main Road 并希望它选择39 Main Road 在给定列表的地址字段中的记录(完整地址为39 Main Road RICHMOND, NSW, Australia)
但是,我当前的代码错误是:TypeError: unsupported operand type(s) for <<: 'str' and 'CharField'
我尝试将 models.Listing.address 转换为 str 但这只会返回 unsupported operand type(s) for <<: 'str' and 'str'
我会使用address in models.Listing.address,但根据the docs 和this answer,必须使用<< 运算符。
此外,我尝试使用 address.in_(models.Listing.address) 哪个字符串不具有...的属性。
是不是因为我没有比较 CharField 和 CharField?如果是这样,我如何将字符串与 CharField 进行比较?我尝试将 CharField 更改为字符串,但我不能将 in 与 peewee 一起使用,因为正如 @coleifer 在 this answer 的评论中所说:
Python 总是将
x in y的返回值强制为布尔值,因此需要使用<<运算符。
提前致谢!
【问题讨论】:
标签: python sql flask peewee flask-peewee