ユーザー定義の演算子を追加する方法については、peewee ドキュメントの短いガイドに従いましたが、実行しようとすると KeyError が返されます。
from peewee import *
from peewee import OP
from peewee import Expression
db = MySQLDatabase(app.config['MYSQL_DATABASE_DB'], host=app.config['MYSQL_DATABASE_HOST'], user=app.config['MYSQL_DATABASE_USER'], passwd=app.config['MYSQL_DATABASE_PASSWORD'])
OP['MOD'] = 'mod'
def mod(lhs, rhs):
return Expression(lhs, OP.MOD, rhs)
MySQLDatabase.register_ops({OP.MOD: '%'})
class Base(Model):
class Meta:
database = db
class User(Base):
user_id = PrimaryKeyField()
first_name = CharField(max_length = 150)
last_name = CharField(max_length = 150)
@app.route('/')
def test():
query = User.select().where(mod(User.user_id, 2) == 0)
return "Query: %r" % query
実行しようとすると、次のエラーが表示されます。
KeyError: 'mod'
私が間違っているかもしれないアイデアはありますか?