gpt4 book ai didi

python - 从数据存储刷新后的 Google 应用引擎

转载 作者:行者123 更新时间:2023-11-30 23:59:47 25 4
gpt4 key购买 nike

我想在每个 datetime 实例从数据存储区出来后立即自动设置它的 .tzinfo。

如果我有

class Message( db.Model ):
creationTime = db.DateTimeProperty()
someOtherTime = db.DateTimeProperty()

## I really want to define a method like this,
## that runs immediately AFTER an instance has
## been refreshed from the datastore
def wakeup( self ):
self.creationTime.tzinfo = self.creationTime.replace( tzinfo=EST )
self.someOtherTime.tzinfo = self.creationTime.replace( tzinfo=EST )

可以在 GAE 中执行此操作吗?

最佳答案

我认为最好的方法是您子类 DateTimeProperty类并重写其方法make_value_from_datastore :

class EstDateTimeProperty(db.DateTimeProperty):
def make_value_from_datastore(self, value):
naive_utc = db.DateTimeProperty(self, value)
aware_utc = naive_utc.replace(tzinfo=UTC)
return aware_utc.astimezone(EST)

当然给定合适的 tzinfo 对象 UTC 和 EST(由 pytz 或其他构建)。当然,您还可以构建一个更通用的“智能日期时间”属性类,并让它通过其 __init__ 中的关键字参数设置感兴趣的时区,例如,如果您需要使用多个不同的日期时间属性不同时区。

关于python - 从数据存储刷新后的 Google 应用引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2089746/

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