gpt4 book ai didi

python - DateTimeProperty 在 Google App Engine 中设置为日期时间时出错

转载 作者:行者123 更新时间:2023-11-28 22:07:26 25 4
gpt4 key购买 nike

我在编写的一些 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)

关于python - DateTimeProperty 在 Google App Engine 中设置为日期时间时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2042913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com