Database gae ndb查询多个值
我正在GAE上使用python。Database gae ndb查询多个值,database,Database,我正在GAE上使用python。 我有一个查询,需要将字段与列表中的值进行匹配。 我的实体看起来像: class TimeTableChange(ndb.Model): """A Change entry.""" details = ndb.StringProperty() class_label = ndb.StringProperty() class_id = ndb.IntegerProperty() year =
我有一个查询,需要将字段与列表中的值进行匹配。 我的实体看起来像:
class TimeTableChange(ndb.Model):
"""A Change entry."""
details = ndb.StringProperty()
class_label = ndb.StringProperty()
class_id = ndb.IntegerProperty()
year = ndb.IntegerProperty()
month = ndb.IntegerProperty()
day_of_month = ndb.IntegerProperty()
hour = ndb.IntegerProperty()
type = ndb.StringProperty()
ts = ndb.IntegerProperty()
我想运行一个类似(MySQL-like-example)的查询:
列表[12,34,67]只是一个例子。该列表可能为空或包含N个整数。如何创建正确的ndb查询?对于我使用的年/月/日
query = query.filter(ndb.AND(TimeTableChange.year == tomorrow.year,
TimeTableChange.month == tomorrow.month,
TimeTableChange.day_of_month == tomorrow.day))
而且效果很好。现在我需要将“class_id”与列表进行匹配试试这个
query = query.filter(ndb.AND(ndb.AND(TimeTableChange.year == tomorrow.year,
TimeTableChange.month == tomorrow.month,
TimeTableChange.day_of_month == tomorrow.day), TimeTableChange.class_id.IN([12, 34, 67])))
为什么不使用DateTime属性呢
它使用默认的Python日期时间格式。与几个AND或筛选器相比,支持其他日期时间操作的查询速度要快得多。int list呢?它在查询中是如何表示的?我更新了答案,我认为这对int有效。
query = query.filter(ndb.AND(ndb.AND(TimeTableChange.year == tomorrow.year,
TimeTableChange.month == tomorrow.month,
TimeTableChange.day_of_month == tomorrow.day), TimeTableChange.class_id.IN([12, 34, 67])))
class TimeTableChange(ndb.Model):
created_at = ndb.DateTimeProperty()
from datetime import datetime
dateobj = datetime.strptime('Aug 12 2017 1:33PM', '%b %d %Y %I:%M%p')
query = TimeTableChange.query(TimeTableChange.created_at >= dateobj)