【问题标题】:Compare string with CharField with peewee?将字符串与 CharField 与 peewee 进行比较?
【发布时间】:2017-05-01 19:54:14
【问题描述】:

我正在尝试获取给定字符串addressmodels.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 &lt;&lt;: 'str' and 'CharField'

我尝试将 models.Listing.address 转换为 str 但这只会返回 unsupported operand type(s) for &lt;&lt;: 'str' and 'str'

我会使用address in models.Listing.address,但根据the docsthis answer,必须使用&lt;&lt; 运算符。

此外,我尝试使用 address.in_(models.Listing.address) 哪个字符串不具有...的属性。

是不是因为我没有比较 CharField 和 CharField?如果是这样,我如何将字符串与 CharField 进行比较?我尝试将 CharField 更改为字符串,但我不能将 in 与 peewee 一起使用,因为正如 @coleifer 在 this answer 的评论中所说:

Python 总是将x in y 的返回值强制为布尔值,因此需要使用&lt;&lt; 运算符。

提前致谢!

【问题讨论】:

    标签: python sql flask peewee flask-peewee


    【解决方案1】:

    嗯...我很快就会发现:

    x &lt;&lt; y 用于检查记录 x 是否在记录 y 的列表或查询中

    而我试图实现的功能是通过以下方式完成的:

    .where(models.Listing.address.contains(address))

    所以你去吧,未来的我不可避免地会再次这样做;)

    【讨论】:

      猜你喜欢
      • 2012-10-06
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多