Peewee自救问题(更新查询)

时间:2014-12-16 16:13:25

标签: python peewee

我有以下Peewee模型Stock。我正在使用setFetchActive()尝试通话Stock.get(Stock.id == 1).setFetchActive()。但是我正在解决下面的错误。

class Stock(BaseModel):
  id = PrimaryKeyField()
  code = CharField(db_column='code', max_length=16)
  symbol= CharField(db_column='symbol', max_length=64)
  createdAt = DateTimeField(default=datetime.datetime.now,db_column='created_at')
  fetch_active = BooleanField(default=False,db_column = 'fetch_active')
  fetchToggleTime = DateTimeField(null = True,db_column='fetch_toggle_time')
def setFetchActive(self):
    self.fetch_active = True
    self.fetchToggleTime = datetime.datetime.now
    self.save()
    return self

这是错误。 self.save()似乎存在问题。选择很好。我在这里做错了什么?

    KeyError                                  Traceback (most recent call last)
<ipython-input-56-306d3b2f1e40> in <module>()
----> 1 Stock.get(Stock.id == 1).setFetchActive()

<ipython-input-55-2d6a9ec95dab> in setFetchActive(self)
     15         self.fetch_active = True
     16         self.fetchToggleTime = datetime.datetime.now
---> 17         self.save()
     18         return self
     19     class Meta:

C:\Anaconda\lib\site-packages\peewee.pyc in save(self, force_insert, only)
   3555             else:
   3556                 field_dict = self._prune_fields(field_dict, self.dirty_fields)
-> 3557             rows = self.update(**field_dict).where(self._pk_expr()).execute()
   3558         else:
   3559             pk = self._get_pk_value()

C:\Anaconda\lib\site-packages\peewee.pyc in execute(self)
   2529 
   2530     def execute(self):
-> 2531         return self.database.rows_affected(self._execute())
   2532 
   2533 class InsertQuery(Query):

C:\Anaconda\lib\site-packages\peewee.pyc in _execute(self)
   2162     def _execute(self):
   2163         sql, params = self.sql()
-> 2164         return self.database.execute_sql(sql, params, self.require_commit)
   2165 
   2166     def execute(self):

C:\Anaconda\lib\site-packages\peewee.pyc in execute_sql(self, sql, params, require_commit)
   2759             cursor = self.get_cursor()
   2760             try:
-> 2761                 cursor.execute(sql, params or ())
   2762             except Exception as exc:
   2763                 if self.get_autocommit() and self.autorollback:

C:\Anaconda\lib\site-packages\pymysql\cursors.pyc in execute(self, query, args)
    128 
    129         if args is not None:
--> 130             query = query % self._escape_args(args, conn)
    131 
    132         result = self._query(query)

C:\Anaconda\lib\site-packages\pymysql\cursors.pyc in _escape_args(self, args, conn)
     94     def _escape_args(self, args, conn):
     95         if isinstance(args, (tuple, list)):
---> 96             return tuple(conn.escape(arg) for arg in args)
     97         elif isinstance(args, dict):
     98             return dict((key, conn.escape(val)) for (key, val) in args.items())

C:\Anaconda\lib\site-packages\pymysql\cursors.pyc in <genexpr>((arg,))
     94     def _escape_args(self, args, conn):
     95         if isinstance(args, (tuple, list)):
---> 96             return tuple(conn.escape(arg) for arg in args)
     97         elif isinstance(args, dict):
     98             return dict((key, conn.escape(val)) for (key, val) in args.items())

C:\Anaconda\lib\site-packages\pymysql\connections.pyc in escape(self, obj)
    688         if isinstance(obj, str_type):
    689             return "'" + self.escape_string(obj) + "'"
--> 690         return escape_item(obj, self.charset)
    691 
    692     def literal(self, obj):

C:\Anaconda\lib\site-packages\pymysql\converters.pyc in escape_item(val, charset)
     22     if type(val) is dict:
     23         return escape_dict(val, charset)
---> 24     encoder = encoders[type(val)]
     25     val = encoder(val)
     26     if type(val) in [str, int, text_type]:

KeyError: <type 'builtin_function_or_method'>

1 个答案:

答案 0 :(得分:1)

这是这一行:

self.fetchToggleTime = datetime.datetime.now

应该是:

self.fetchToggleTime = datetime.datetime.now()

仔细阅读追溯:)