简体   繁体   中英

Peewee Foreignkeyfield doesn't accept appropriate foreign key field

I'm using the Python Peewee ORM in my flask app as a database. 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. The weird thing is that I defined the field to be of type ForeignKeyField(User) , but it appears to ask for a string/number.

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. When I print type(g.user) I get the type <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 . The User class looks as follows:

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:

@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? 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:

ticket.owner = g.user._get_current_object()

Saving this in my Ticket model works like a charm.

ps. Since this works, I use it now. But if there's anything wrong with this approach, I would love to hear it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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