Peewee Foreignkeyfield不接受适当的外键领域

时间:2014-10-03 15:07:56

标签: python database orm flask peewee

我在我的烧瓶应用程序中使用Python Peewee ORM作为数据库。我有一个用户定义和票证如下:

class Ticket(db.Model):
    created = DateTimeField(default=datetime.utcnow)
    event_id = CharField()
    owner = ForeignKeyField(User, related_name='owned_tickets')

然后我尝试按如下方式保存票证:

ticket = Ticket()
ticket.event_id=request.form['eventId']
ticket.owner=g.user
ticket.save()

但是我得到了一个TypeError: int() argument must be a string or a number, not 'User',其中有一个(巨大的)堆栈跟踪,我粘贴在下面。奇怪的是我将字段定义为类型ForeignKeyField(User),但它似乎要求字符串/数字。

有人知道我在这里做错了什么吗?欢迎所有提示!

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Library/Python/2.7/site-packages/flask_login.py", line 758, in decorated_view
    return func(*args, **kwargs)
  File "/Users/kramer65/dev/repos/tc/app/views/webviews.py", line 120, in myTickets
    ticket.save()
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 3511, in save
    pk_from_cursor = self.insert(**field_dict).execute()
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 2565, in execute
    return self.database.last_insert_id(self._execute(), self.model_class)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 2126, in _execute
    sql, params = self.sql()
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 2556, in sql
    return self.compiler().generate_insert(self)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1530, in generate_insert
    return self.build_query(clauses, alias_map)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1359, in build_query
    return self.parse_node(Clause(*clauses), alias_map)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1320, in parse_node
    sql, params, unknown = self._parse(node, alias_map, conv)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1295, in _parse
    sql, params = self._parse_map[node_type](node, alias_map, conv)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1248, in _parse_clause
    node.nodes, alias_map, conv, node.glue)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1337, in parse_node_list
    node_sql, node_params = self.parse_node(node, alias_map, conv)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1320, in parse_node
    sql, params, unknown = self._parse(node, alias_map, conv)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1295, in _parse
    sql, params = self._parse_map[node_type](node, alias_map, conv)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1248, in _parse_clause
    node.nodes, alias_map, conv, node.glue)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1337, in parse_node_list
    node_sql, node_params = self.parse_node(node, alias_map, conv)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1320, in parse_node
    sql, params, unknown = self._parse(node, alias_map, conv)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1295, in _parse
    sql, params = self._parse_map[node_type](node, alias_map, conv)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1248, in _parse_clause
    node.nodes, alias_map, conv, node.glue)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1337, in parse_node_list
    node_sql, node_params = self.parse_node(node, alias_map, conv)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1320, in parse_node
    sql, params, unknown = self._parse(node, alias_map, conv)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1295, in _parse
    sql, params = self._parse_map[node_type](node, alias_map, conv)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1236, in _parse_param
    params = [node.conv(node.value)]
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1052, in db_value
    return self.to_field.db_value(value)
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 674, in db_value
    return value if value is None else self.coerce(value)
  File "/Library/Python/2.7/site-packages/werkzeug/local.py", line 390, in <lambda>
    __int__ = lambda x: int(x._get_current_object())
TypeError: int() argument must be a string or a number, not 'User'

[编辑] 好的,错误似乎位于其他地方,而不是我到目前为止发布的代码。我尝试从python交互式解释器添加一个Ticket,它运行得非常好。当我print type(g.user)时,我会收到类型<class 'werkzeug.local.LocalProxy'>。这很奇怪,因为我能够打印出正确的g.user.usernameg.user.passwordUser类如下所示:

class User(relDb.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)

我按如下方式设置g.user对象:

@app.before_request
def before_request():
    g.user = current_user

有没有人知道为什么g.user提供werkzeug.local.LocalProxy而不是User类?欢迎所有提示!

1 个答案:

答案 0 :(得分:1)

好的,经过多次阅读后,我找到了解决方案。由于g永远不会返回底层对象,而只是作为将命令传递给实际对象并返回结果的代理,我需要获取g.user点的对象,这样做就像这样:

ticket.owner = g.user._get_current_object()

将此保存在我的故障单模型中就像魅力一样。

PS。既然这样可行,我现在就用它。但如果这种方法有任何问题,我很乐意听到。