Flask登录get_id方法给Peewee带来错误

时间:2014-09-18 13:32:42

标签: python flask peewee flask-login

我试图将Peewee与Flask-login结合使用,现在我在实施get_id()方法时遇到了麻烦。在the example as given by this tutorial之后,我的用户类如下:

class User(db.Model, BaseUser):
    username = CharField()
    password = CharField()

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return unicode(self.id)

尝试将用户保存到(sqlite)数据库时,出现以下错误:

>>> from app.models import User
>>> u = User()
>>> u.username = 'lala'
>>> u.password = 'blabla'
>>> u.save()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/peewee.py", line 3488, in save
    rows = self.update(**field_dict).where(self.pk_expr()).execute()
  File "/Library/Python/2.7/site-packages/peewee.py", line 2487, in execute
    return self.database.rows_affected(self._execute())
  File "/Library/Python/2.7/site-packages/peewee.py", line 2119, in _execute
    sql, params = self.sql()
  File "/Library/Python/2.7/site-packages/peewee.py", line 2484, in sql
    return self.compiler().generate_update(self)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1482, in generate_update
    return self.build_query(clauses, alias_map)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1357, in build_query
    return self.parse_node(Clause(*clauses), alias_map)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1318, in parse_node
    sql, params, unknown = self._parse(node, alias_map, conv)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1293, in _parse
    sql, params = self._parse_map[node_type](node, alias_map, conv)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1246, in _parse_clause
    node.nodes, alias_map, conv, node.glue)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1335, in parse_node_list
    node_sql, node_params = self.parse_node(node, alias_map, conv)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1318, in parse_node
    sql, params, unknown = self._parse(node, alias_map, conv)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1293, in _parse
    sql, params = self._parse_map[node_type](node, alias_map, conv)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1227, in _parse_expression
    rhs, rparams = self.parse_node(node.rhs, alias_map, conv)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1320, in parse_node
    params = [conv.db_value(i) for i in params]
  File "/Library/Python/2.7/site-packages/peewee.py", line 674, in db_value
    return value if value is None else self.coerce(value)
ValueError: invalid literal for int() with base 10: 'None'

如果我删除get_id()方法,我没有错误,因此该方法似乎是所有邪恶的来源。有人知道它有什么问题吗?欢迎所有提示!

1 个答案:

答案 0 :(得分:2)

Peewee模型实现了一个get_id()方法,该方法由pk_expr()方法使用。我猜这是问题的根源。

修复了主文件:https://github.com/coleifer/peewee/issues/435