NDB 按 DateTimeProperty 的时间部分查询
NDB query by time part of DateTimeProperty
我需要查询在一天中的某个时间添加的项目,忽略哪一天。我只保存添加项目时的 DateTime
。
比较datetime.Time
和DateTimeProperty
会报错,而且DateTimeProperty没有time()
方法。
唯一的方法是将一天中的时间存储为单独的 属性。一个 int 就可以了,你可以将它存储为秒。您可以明确地执行此操作(即在设置日期时间的同时设置时间 属性,或使用计算的 属性 自动设置该值。
我有同样的问题(或多或少),我认为唯一的方法是构建我们自己的日期 class 模型。
像这样:
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()
...
但我也认为我们应该有其他方法来更容易地做到这一点。
我需要查询在一天中的某个时间添加的项目,忽略哪一天。我只保存添加项目时的 DateTime
。
比较datetime.Time
和DateTimeProperty
会报错,而且DateTimeProperty没有time()
方法。
唯一的方法是将一天中的时间存储为单独的 属性。一个 int 就可以了,你可以将它存储为秒。您可以明确地执行此操作(即在设置日期时间的同时设置时间 属性,或使用计算的 属性 自动设置该值。
我有同样的问题(或多或少),我认为唯一的方法是构建我们自己的日期 class 模型。 像这样:
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()
...
但我也认为我们应该有其他方法来更容易地做到这一点。