Python-SQLAlchemy和Postgres如何在json值中使用子字符串进行搜索

2024-09-28 03:23:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道我可以使用以下示例查询轻松查询JSON值:

r = Books.query.filter(
  Books.nameofjsonfield['key1', 'key2'].astext.cast(Unicode) == 'exact_string_to_compare'
).all()

但如何搜索与JSON值的子字符串匹配的数据集呢


Tags: tojson示例stringunicodefilterquerybooks
1条回答
网友
1楼 · 发布于 2024-09-28 03:23:58

将值转换为文本后,只需using ^{} one way or the other或任何其他模糊匹配方法:

r = Books.query.\
    filter(Books.nameofjsonfield['key1', 'key2'].
           astext.
           cast(Unicode).
           contains('not_so_exact_string_to_find')).\
    all()

相关问题 更多 >

    热门问题