2

我有ndb模式其中有字符串重复属性。我试图检索所有具有空值的实体。但NDB查询返回空。NDB查询空字符串重复StringProperty

class A(ndb.model): 
    name = ndb.StringProperty() 
    values = ndb.StringProperty(repeated=True) 

a1 = A() 
a1.name = "T1" 
a1.values = ['V1', 'V2'] 
a1.put() 

a2 = A() 
a2.name = "T2" 
a2.values = [] 
a2.put() 

result = A.query(A.values=="") # Return empty 
result = A.query(A.values==[]) # BadValueError: Expected string, got [] 

for each in result: 
    print each.name 

如何查询具有空/无值的实体?

回答

2

我认为你必须立足于持有值的数量另一个领域,例如查询,

num_values = ndb.IntegerProperty(indexed=True) 

你会在每次更新value场时间来更新这个数字。然后你就可以查询这样的:

result = A.query(A.num_values==0) 

这类似于另一个问题:NDB: Sort query results