简体   繁体   English

Peewee 在尝试添加用户定义的运算符时抛出 KeyError

[英]Peewee throws KeyError when trying to add user defined operator

I followed the short guide in the peewee docs on how to add user defined operators, but when I try to do it, it gives me a KeyError.我遵循了 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

When I try to run it, it gives me this error:当我尝试运行它时,它给了我这个错误:

KeyError: 'mod'

Any ideas what I might be doing wrong?任何想法我可能做错了什么?

The problem is that you are defining your database before you are calling register_ops() .问题是您在调用register_ops()之前定义了数据库。 To fix the immediate bug you can move your db = MySQL... below the call to register_ops() .要修复直接错误,您可以将db = MySQL...移动到register_ops()调用下方。

This does seem a little like a bug in peewee, though, so I've opened a github issue: https://github.com/coleifer/peewee/issues/599不过,这确实有点像 peewee 中的错误,所以我打开了一个 github 问题: https : //github.com/coleifer/peewee/issues/599


Edit: I decided to after all not change the behavior.编辑:我决定毕竟不改变行为。 The solution I proposed should work, though -- register ops first, instantiate second.不过,我提出的解决方案应该可行——首先注册操作,然后实例化。 You can also specify custom ops when instantiating the database:您还可以在实例化数据库时指定自定义操作:

http://docs.peewee-orm.com/en/latest/peewee/api.html#Database http://docs.peewee-orm.com/en/latest/peewee/api.html#Database

Example例子

OP['MOD'] = 'mod'

def mod(lhs, rhs):
    return Expression(lhs, OP.MOD, rhs)

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'],
    ops={OP.MOD: '%'})

Before this在这之前

def mod(lhs, rhs):
    return Expression(lhs, OP.MOD, rhs)

You need to define this你需要定义这个

OP['MOD'] = 'mod'
OP['MOD'] = 'mod'

def mod(lhs, rhs):
    return Expression(lhs, OP.MOD, rhs)

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'])

Specifying the ops argument will lead to error, in fact, you don't have to do that.指定ops参数会导致错误,实际上,您不必这样做。

The only thing you have to be careful is to import the function mod you defined to the module that you want to use it in.您唯一需要注意的是将您定义的函数mod导入到您要使用它的模块中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM