Python Peewee动态生成where子句以检查重复项

Python Peewee动态生成where子句以检查重复项,python,mysql,peewee,Python,Mysql,Peewee,我目前正在python项目中使用peewee作为ORM。 我试图确定给定的一组对象,数据库中是否已经存在这些对象。对于唯一性基于一个简单键的对象,我可以生成一个键列表并在数据库中进行选择: Model.select()? clauses = [] for obj in db_objects: # get_unique_key returns a tuple of the all the column values # that determine

我目前正在python项目中使用peewee作为ORM。 我试图确定给定的一组对象,数据库中是否已经存在这些对象。对于唯一性基于一个简单键的对象,我可以生成一个键列表并在数据库中进行选择:


Model.select()?
    clauses = []
    for obj in db_objects:
        # get_unique_key returns a tuple of the all the column values
        # that determine uniqueness for this object.
        uniq_key = self._get_unique_key(obj)
        subclause = [getattr(self.model_class, uniq_column) == value
                     for uniq_column, value in zip(self.uniq_columns, uniq_key)]

        clauses.append(reduce(operator.and_, subclause))

    dups = self.model_class.select().where(reduce(operator.or_, clauses)).execute()