【问题标题】:NDB query by time part of DateTimeProperty按 DateTimeProperty 的时间部分进行 NDB 查询
【发布时间】:2015-09-05 19:32:16
【问题描述】:

我需要查询在一天中的某个时间添加的项目,忽略哪一天。我只保存添加项目时的DateTime

比较datetime.TimeDateTimeProperty 会出错,并且DateTimeProperty 没有time() 方法。

【问题讨论】:

    标签: python google-app-engine datetime google-cloud-datastore app-engine-ndb


    【解决方案1】:

    唯一的方法是将一天中的时间存储为单独的属性。一个 int 就可以了,你可以将它存储为秒。您可以显式执行此操作(即在设置日期时间的同时设置时间属性,或使用计算属性自动设置值。

    【讨论】:

      【解决方案2】:

      我有同样的问题(或多或少),我认为唯一的方法是建立我们自己的日期类模型。 像这样的:

      class Date(ndb.model):
         ...
         day = ndb.IntegerProperty()       
         month = ndb.IntegerProperty()
         year = ndb.IntegerProperty()             
         ...
      
      class Anything(ndb.Model):
         ...
         dateTime = ndb.StructuredProperty(Date)
         ...
      

      代替:

      class Anything(ndb.Model):
         ...
         dateTime = ndb.DateTimeProperty()
         ...
      

      但我也认为我们应该有其他方法来更轻松地做到这一点。

      【讨论】:

        猜你喜欢
        • 2013-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-26
        • 2014-10-08
        • 1970-01-01
        • 1970-01-01
        • 2013-08-07
        相关资源
        最近更新 更多