DateTimeProperty在Google App Engine中将错误设置为日期时间
我在写一些Google App Engine代码时出现了一个奇怪的错误。
我的程序包含这样的一些代码:
import datetime
...
class Action(db.Model):
visibleDate = db.DateTimeProperty()
...
getActionQuery = Action.gql("WHERE user = :user AND __key__ = :key", user = user, key = self.request.get("key"))
theAction = getActionQuery.get()
....
theAction.visibleDate = datetime.datetime.strptime(self.request.get("visibleDate"), "%Y/%m/%d")
但是这会产生以下错误:
Traceback (most recent call last): File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/__init__.py", line 509, in __call__ handler.post(*groups) File "/Users/redbird/Developer/betterdo-it/main.py", line 132, in post theAction.visibleDate = datetime.datetime.strptime(self.request.get("visibleDate"), "%Y/%m/%d"), File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/db/__init__.py", line 472, in __set__ value = self.validate(value) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/db/__init__.py", line 2308, in validate (self.name, self.data_type.__name__)) BadValueError: Property visibleDate must be a datetime
任何想法为什么发生这种情况? 我已经测试过它,并且我知道我的时间正在进入,正在转换正确,但是却遇到了这个错误。
我认为在追踪中你错过了一些东西。
我看到: datetime.datetime.strptime(self.request.get("visibleDate"), "%Y/%m/%d"),
注意行尾的逗号。
该逗号使得该行返回一个包含你日期的元组。 我假设你不小心添加了逗号,所以只需将其删除,并且您应该正确指定日期时间。
回顾:
from datetime import datetime
a = (datetime(2000,1,1),)
assert isinstance(a, tuple)
a = (datetime(2000,1,1))
assert isinstance(a, datetime)
链接地址: http://www.djcxy.com/p/88685.html
上一篇: DateTimeProperty has error being set to a datetime in Google App Engine