DateTimeProperty has error being set to a datetime in Google App Engine

I'm having a weird error with some Google App Engine code I'm writing.

My program contains some code like this:

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")

Yet this produces the following error:

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

Any ideas on why this is happening? I've tested it and I know that my time is coming in, is getting converted correctly, but then hits this error.


I think there's something you missed in your traceback.

I'm seeing: datetime.datetime.strptime(self.request.get("visibleDate"), "%Y/%m/%d"),

Notice the comma at the end of the line.

That comma makes that line return a tuple with your date inside it. I'm assuming you accidentally added the comma, so just remove it and you should be correctly assigning a datetime.

To review:

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/88686.html

上一篇: App Engine:Handler.py中的TypeError

下一篇: DateTimeProperty在Google App Engine中将错误设置为日期时间