我正在努力让烧瓶恢复以返回一个日期时间字段,但它会出现错误:
MarshallingException: 'datetime.date' object has no attribute 'utctimetuple'
模型(sqlalchemy)如下:
class Target(db.Model):
id = db.Column(db.Integer, primary_key=True)
uuid = db.Column(db.String(256))
title = db.Column(db.String(80))
description = db.Column(db.String(250))
done = db.Column(db.Boolean, default = False)
startDate = db.Column(db.DateTime, default=datetime.utcnow)
timeDuration = db.Column(db.Integer, default = 0)
isActive = db.Column(db.Boolean, default = True)
目标字段为:
target_fields = {
'id': fields.Integer,
'uuid' : fields.String,
'title': fields.String,
'description': fields.String,
'done': fields.Boolean,
'startDate' : fields.DateTime
# 'uri': fields.Url('target')
}
实际的休息资源是:
class TargetAPI(Resource):
decorators = [auth.login_required]
def __init__(self):
self.reqparse = reqparse.RequestParser()
self.reqparse.add_argument('title', type = str, location = 'json')
self.reqparse.add_argument('description', type = str, location = 'json')
self.reqparse.add_argument('done', type = bool, location = 'json')
self.reqparse.add_argument('startDate', type = datetime, location = 'json')
self.reqparse.add_argument('timeDuration', type = int, location = 'json')
self.reqparse.add_argument('isActive', type = bool, location = 'json')
super(TargetAPI, self).__init__()
def get(self,username, id):
target = Target.query.get(id)
if not target:
abort(404)
return { 'target': marshal(target, target_fields) }
完整堆栈跟踪是:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 258, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 258, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 397, in wrapper
resp = resource(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/flask_httpauth.py", line 53, in decorated
return f(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 487, in dispatch_request
resp = meth(*args, **kwargs)
File "/Users/nahuel/proj/ptcp/app/resources/targetListAPI.py", line 60, in post
'target': marshal(newTarget, target_fields) }, 201
File "/usr/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 533, in marshal
return OrderedDict(items)
File "/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/collections.py", line 52, in __init__
self.__update(*args, **kwds)
File "/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_abcoll.py", line 547, in update
for key, value in other:
File "/usr/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 532, in <genexpr>
for k, v in fields.items())
File "/usr/local/lib/python2.7/site-packages/flask_restful/fields.py", line 109, in output
return self.format(value)
File "/usr/local/lib/python2.7/site-packages/flask_restful/fields.py", line 271, in format
raise MarshallingException(ae)
我不能为我的生活理解为什么它试图访问datetime.date,因为我总是使用datetime.datetime对象!有什么想法吗?
答案 0 :(得分:2)
最后解决了这个问题!为了完整起见,我将发布答案: 本质上,问题是下面的postgresql数据库使用'startDate'字段的日期类型,而不是时间戳1。因此,当sqlalchemy检索到Target.startDate对象时,它会为其分配datetime.date类型而不是datetime.datetime。
在:
Table "public.target"
Column | Type | Modifiers | Storage | Stats target | Description
--------------+------------------------+-----------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('target_id_seq'::regclass) | plain | |
uuid | character varying(256) | | extended | |
title | character varying(80) | | extended | |
description | character varying(250) | | extended | |
done | boolean | | plain | |
startDate | date | | plain | |
timeDuration | integer | | plain | |
isActive | boolean | | plain | |
userId | integer | | plain | |
后:
startDate | timestamp without time zone | | plain | |