how to pass a not like operator in a sqlalchemy ORM query

OrmSqlalchemyNegation

Orm Problem Overview


I've got a query:

MyModel.query.filter(Mymodel.name.contains('a_string'))

I need to do the same query but with the negation (a not like operator) but didn't find any operator matching my need in the http://www.sqlalchemy.org/docs/orm/tutorial.html#common-filter-operators">SQLAlchemy documentation.

Is there any way to do it without using the sql part of SQLAlchemy???

Orm Solutions


Solution 1 - Orm

Just negate the filter:

MyModel.query.filter(sqlalchemy.not_(Mymodel.name.contains('a_string')))

Solution 2 - Orm

There is now a notlike() method. Couldn't find it in the docs but it exists!

MyModel.query.filter(Mymodel.name.notlike('%a_string%'))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJérôme PigeotView Question on Stackoverflow
Solution 1 - OrmMaxim SloykoView Answer on Stackoverflow
Solution 2 - OrmJossyView Answer on Stackoverflow