使用字段组合在 python peewee 中查询

Querying in python peewee using combination of fields

我有一个带有字段 name:string 和 id_no:integer 的 table,我从外部来源获得名称列表和 id_no,我需要使用此组合使用 peewee ORM。

例如:

input = [
{'name':'name1', 'id_no': 1},
{'name':'name2', 'id_no': 2},
{'name':'name3', 'id_no': 3},
]

我要写什么查询来获取包含上述数据组合的记录?

mysql 中的类似查询:

SELECT * FROM table_name
WHERE CONCAT(convert(id_no, char), ':', name) IN ('1:name1','2:name2','3:name3')

我会写成:

import operator

data = [
  {'name':'name1', 'id_no': 1},
  {'name':'name2', 'id_no': 2},
  {'name':'name3', 'id_no': 3},
]
conditions = [
  ((MyModel.name == item['name']) & (MyModel.id_no == item['id_no']))
  for item in data]
expr = reduce(operator.or_, conditions)
query = MyModel.select().where(expr)