Adding user-defined operators
Because I ran out of python operators to overload, there are some missingoperators in peewee, for instance modulo
. If you find that you need tosupport an operator that is not in the table above, it is very easy to add yourown.
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.