我知道我可以使用这个示例查询轻松查询 JSON 值:
r = Books.query.filter(
Books.nameofjsonfield['key1', 'key2'].astext.cast(Unicode) == 'exact_string_to_compare'
).all()
但我如何搜索与 JSON 值的子字符串匹配的数据集?
最佳答案
将值转换为文本后,很简单using LIKE
one way or the other ,或任何其他模糊匹配方法:
r = Books.query.\
filter(Books.nameofjsonfield['key1', 'key2'].
astext.
cast(Unicode).
contains('not_so_exact_string_to_find')).\
all()
关于Python SQLAlchemy 和 Postgres - 如何使用 json 值中的子字符串进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59055834/