[英]Peewee Foreignkeyfield doesn't accept appropriate foreign key field
I'm using the Python Peewee ORM in my flask app as a database. 我在我的烧瓶应用程序中使用Python Peewee ORM作为数据库。 I've got a user defined and a ticket as follows:
我有一个用户定义的票证,如下所示:
class Ticket(db.Model):
created = DateTimeField(default=datetime.utcnow)
event_id = CharField()
owner = ForeignKeyField(User, related_name='owned_tickets')
I then try to save a ticket as follows: 然后,我尝试如下保存票证:
ticket = Ticket()
ticket.event_id=request.form['eventId']
ticket.owner=g.user
ticket.save()
but I get a TypeError: int() argument must be a string or a number, not 'User'
with a (huge) stacktrace which I pasted below. 但是我收到
TypeError: int() argument must be a string or a number, not 'User'
带有(巨大)堆栈跟踪的TypeError: int() argument must be a string or a number, not 'User'
,我在下面粘贴了该跟踪。 The weird thing is that I defined the field to be of type ForeignKeyField(User)
, but it appears to ask for a string/number. 奇怪的是,我将字段定义为
ForeignKeyField(User)
类型,但似乎要求输入字符串/数字。
Does anybody know what I'm doing wrong here? 有人知道我在做什么错吗? All tips are welcome!
欢迎所有提示!
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'
[EDIT] Okay, the error seems to be located somewhere else than in the code I posted so far. [编辑]好的,该错误似乎位于我到目前为止发布的代码中之外的其他地方。 I tried adding a Ticket from the python interactive interpreter, which works perfectly well.
我尝试从python交互式解释器中添加票证,该票证效果很好。 When I
print type(g.user)
I get the type <class 'werkzeug.local.LocalProxy'>
. 当我
print type(g.user)
我得到类型<class 'werkzeug.local.LocalProxy'>
print type(g.user)
<class 'werkzeug.local.LocalProxy'>
。 This is pretty weird, because I am able to simply print out the correct g.user.username
and g.user.password
. 这很奇怪,因为我能够简单地打印出正确的
g.user.username
和g.user.password
。 The User
class looks as follows: User
类如下所示:
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)
I set the g.user
object as follows: 我将
g.user
对象设置如下:
@app.before_request
def before_request():
g.user = current_user
Does anybody have any other idea of why g.user
gives a werkzeug.local.LocalProxy
instead of a User class? 有人对
g.user
为什么给出werkzeug.local.LocalProxy
而不是User类有其他想法吗? All tips are welcome! 欢迎所有提示!
Okay, after some more reading I found the solution. 好吧,经过一番阅读之后,我找到了解决方案。 Since
g
never returns the underlying objects, but merely acts as a proxy for passing on commands to the actual object and returning the results, I need to get the object to which g.user
points, which is done like so: 由于
g
从不返回基础对象,而只是充当将命令传递给实际对象并返回结果的代理,因此我需要获取g.user
指向的对象,方法如下:
ticket.owner = g.user._get_current_object()
Saving this in my Ticket model works like a charm. 将其保存在我的票证模型中就像一种魅力。
ps. PS。 Since this works, I use it now.
由于这有效,所以我现在就使用它。 But if there's anything wrong with this approach, I would love to hear it.
但是,如果这种方法有什么问题,我很想听听。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.