Search code examples
pythonormpeewee

Multiple substrings not in column, with peewee orm


I'm using the Peewee ORM with a sqlite table. I have the following sqllite statement:

SELECT * FROM mytable WHERE name LIKE  '% string1 %' or name LIKE  '%string2 %' OR name LIKE '%string3 %' 

How can I do this in Peewee ?

Note I have read http://docs.peewee-orm.com/en/latest/peewee/querying.html#query-operators , but not sure how to apply this here.


Solution

  • You will want:

    MyModel.select().where(
        MyModel.name.contains('string1') |
        MyModel.name.contains('string2') |
        MyModel.name.contains('string3'))
    

    Or, use case-insensitive LIKE directly:

    MyModel.select().where(
        (MyModel.name ** '%string1%') |
        (MyModel.name ** '%string2%') |
        (MyModel.name ** '%string3%'))
    

    To do a NOT IN:

    MyModel.select().where(MyModel.name.not_in(['string1', 'string2']))