Mod function or escape #177

andmart opened this Issue Mar 26, 2013 · 2 comments


None yet

2 participants

andmart commented Mar 26, 2013

I can't do a query where have to recover only even numbers.

Something like that: % 2 == 0)

This way, peewee 'translates' % for GLOB.

So, is there a way to escape % operator or a peewee.fn.Mod function?


Unfortunately, peewee does not support modulo out of the box (because I ran out of operators to overload).

You can create your own operators, though...perhaps I should add this to the docs. There are some cool examples in playhouse.postgresql_ext where I add operators for various hstore functions. Here's how you do modulo and regexp:

from peewee import *
from peewee import Expr

def mod(lhs, rhs):
    return Expr(lhs, 'mod', rhs)

def regexp(lhs, rhs):
    return Expr(lhs, 're', rhs)

# tell sqlite what to do with these expressions
SqliteDatabase.register_ops({'mod': '%', 're': 'REGEXP'})

class User(Model):
    username = CharField()

# users whose username is all numbers, '[0-9]+')

# users with even ids, 2) == 0)
@coleifer coleifer closed this Mar 29, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment