我正在撰写的一些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
有关为何发生这种情况的任何想法?我已经测试了它,我知道我的时间进入,正在正确转换,但随后出现了这个错误。
答案 0 :(得分:5)
我认为你的追溯中错过了一些东西。
我看到了: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)