tortoise_orm 通过传入字典实现或查询

Nov 14, 2023

# 创建包含查询条件的字典
conditions = {
    'field1': 'value1',
    'field2': 'value2',
}
# 构建 OR 方法一
q_objects = Q()
q_objects.filters = conditions
q_objects.join_type = "OR"
# 构建 OR 方法二
q_objects = Q()
for key, value in conditions.items():
    q_objects |= Q(**{key: value})

# 执行查询
results = await MyModel.filter(q_objects)