python - Flask 登录 get_id 方法给出 Peewee 错误

标签 python flask peewee flask-login

我正在尝试将 peewee 与 Flask-login 结合使用,但现在在实现 get_id() 方法时遇到了麻烦。正在关注the example as given by this tutorial ,我的 User 类如下:

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() 方法,我不会收到任何错误,因此该方法似乎是万恶之源。有人知道这是怎么回事吗?欢迎所有提示!

最佳答案

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

已在主版中修复:https://github.com/coleifer/peewee/issues/435

关于python - Flask 登录 get_id 方法给出 Peewee 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25914172/

相关文章:

python - 如何将对象实例传递给 SocketServer.BaseRequestHandler 实例?

javascript - 具有轮询周期的不同 ajax 调用

python - python中的语法错误;意外缩进

Python:针对不同特定工作人员的分布式任务队列

python - 带有嵌套过滤器的Elasticsearch-dsl以及完全匹配的AND和OR条件

python - 使用 Peewee 和 SQLite 格式化日期和时间

python - 我可以将方法和属性添加到派生自 peewee.Model 的类吗

php - C++代码的Web前端

python - Flask RestPlus : how to catch all exceptions and output the original error

mysql - 如何使用 Peewee ORM 连接到 WebFaction 上的 MySQL 数据库?