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

Python SQLAlchemy和Postgres-如何在json值中使用子字符串进行搜索,python,json,postgresql,sqlalchemy,Python,Json,Postgresql,Sqlalchemy,我知道我可以使用以下示例查询轻松查询JSON值: r = Books.query.filter( Books.nameofjsonfield['key1', 'key2'].astext.cast(Unicode) == 'exact_string_to_compare' ).all() 但是如何搜索与JSON值的子字符串匹配的数据集呢?将值转换为文本后,只需使用以下任何一种模糊匹配方法: r = Books.query.\ filter(Books.nameofjsonfield

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

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

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

将值转换为文本后,只需使用以下任何一种模糊匹配方法:

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