python - 如何按子字符串过滤 JSONB 字段(SQLAlchemy、Postgresql)
问题描述
我想使用 SQLAlchemy 过滤包含子字符串的 JSONB 字段。
我知道我可以使用 contains() 找到完全相同的键,但我想找到该键的子字符串。就像使用带有文本字段的 ilike() 一样。我想对列、收件人和参数都这样做。但是,如果我会得到帮助,我会在第二列中找出如何做到这一点。
class NotificationMixin:
id = Column(Integer, primary_key=True)
recipient = Column(JSONB, nullable=False)
params = Column(JSONB, default={})
class EmailResource(AdminDataTableResource):
def _apply_filters(self, emails, filters):
for key in filters.items():
f_name = key[0]
f_val = key[1]
if len(f_val) > 0:
if f_name == 'recipient':
emails = emails.filter(Email.recipient.contains('"' + f_val + '"'))
if f_name == 'shipment_number':
emails = emails.filter(Email.params["shipment_number"].contains('"' + f_val + '"'))
我可以使用 contains() 以某种方式表现得像 ilike() 吗?如果没有,在我的情况下如何过滤子字符串?有任何想法吗?
解决方案
推荐阅读
- bash - 如果存在单词,如何替换世界,否则使用 sed 在行尾附加一个单词
- python - 归并排序递归版本直觉落后
- wifi - 通过 WiFi 将数据从 ESP32 发送到服务器
- vue.js - 当我尝试安装使用 yarn add ../library 创建到我的应用程序的本地 vue js 库时,找不到依赖项
- node.js - 为什么我在 S3 中的图像保存为字符(����94qgs��jX����z)而不是图像?
- c# - 带有子查询的 SQL 查询
- php - 如果其中任何一个键的值为 NULL,Laravel 集合将删除所有键
- java - 检查 Firebase 数据库集合中是否存在文档名称
- python - 可以在训练过程中使用 keras 实时获取输出层吗?
- python - 如何使用 Shapely 获取 MultiPolygon 内每个点的列表