在保存之前修改数据,在flask-rest-jsonapi中使用before_create_object

时间:2017-06-03 06:34:42

标签: python flask flask-sqlalchemy flask-restful json-api

我想在使用 flask-rest-jsonapi 保存到数据库之前,将时区天真数据视为时区感知。

这是我现在尝试过的:

<element class="Premium">

但仍然没有得到保存。 最终输出是:

class EventList(ResourceList):
    def before_create_object(self, data, view_kwargs):
       starts_at = data['starts_at']
       ends_at = data['ends_at']
       zone = data['timezone']
       local = timezone(zone)
       start_time = local.localize(starts_at)
       end_time = local.localize(ends_at)
       data['starts_at'] = start_time
       data['ends_at'] = end_time
       print ("starts_at :"+str(starts_at)+"\n"+
              "ends_at :"+str(ends_at)+"\n"+
              "start_time :" + str(data['starts_at'])+"\n"+
               "end_time :" 
               +str(data['ends_at'])+str(type(data['ends_at'])))

数据库中的数据保存为:

starts_at :2017-07-06 11:22:33
ends_at :2017-07-16 11:22:33
start_time :2017-07-06 11:22:33+05:30
end_time :2017-07-16 11:22:33+05:30<type 'datetime.datetime'>
/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py:2029: SAWarning: Usage of the 'collection append' operation is not currently supported within the execution stage of the flush process. Results may not be consistent.  Consider using alternative event listeners or connection-level operations instead.
  % method)
/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py:2029: SAWarning: Usage of the 'related attribute set' operation is not currently supported within the execution stage of the flush process. Results may not be consistent.  Consider using alternative event listeners or connection-level operations instead.
  % method)

但它应该是:

  "ends_at": "2017-07-16T05:52:33+00:00"
  "starts_at": "2017-07-06T05:52:33+00:00"

因为我正在通过 "ends_at": "2017-07-16T05:52:33+05:30" "starts_at": "2017-07-06T05:52:33+05:30" 亚洲/加尔各答

更新

但我现在尝试了一件事,即timezone 它确实改变了主题。见输出:

data['topic'] = 'new topic'

0 个答案:

没有答案