2017-02-19 93 views
0

我在GAE上使用python。
我有一个查询,我需要匹配一个字段与来自列表的值。 我的实体的样子:gae ndb查询多个值

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)) 

,它工作正常。现在我需要匹配“类标识码”对一个列表

回答

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]))) 
+0

那么int列表呢?它在查询中如何表示? – balderman

+0

我更新了答案,我认为这将适用于int。 –

0

为什么不使用日期时间属性。

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或过滤器快得多。