我在GAE上使用python 我有一个查询,我需要匹配一个字段与列表中的值。 我的实体看起来像:
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就像示例):
select * from TimeTableChange
where year = 2017 and month = 8 and day_of_month = 12 and class_id IN [12,34,67]
列表[12,34,67]只是一个例子。列表可以为空或包含N个整数。如何创建正确的ndb查询?
对于年/月/ day_of_month,我使用
query = query.filter(ndb.AND(TimeTableChange.year == tomorrow.year,
TimeTableChange.month == tomorrow.month,
TimeTableChange.day_of_month == tomorrow.day))
它工作正常。现在我需要将'class_id'与列表匹配
答案 0 :(得分:1)
试试这个
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])))
答案 1 :(得分:0)
为什么不使用DateTime属性。
https://cloud.google.com/appengine/docs/standard/python/ndb/entity-property-reference#Date_and_Time
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)
它使用默认的Python日期时间格式。比几个AND或过滤器更快的查询支持其他日期时间操作。