Adding user-defined operators
Because I ran out of python operators to overload, there are some missing operators in peewee, for instance modulo
. If you find that you need to support an operator that is not in the table above, it is very easy to add your own.
Here is how you might add support for modulo
in SQLite:
from peewee import *
from peewee import Expression # the building block for expressions
def mod(lhs, rhs):
return Expression(lhs, '%', rhs)
Now you can use these custom operators to build richer queries:
# Users with even ids.
User.select().where(mod(User.id, 2) == 0)
For more examples check out the source to the playhouse.postgresql_ext
module, as it contains numerous operators specific to postgresql’s hstore.