【问题标题】:NDB Query for empty string in repeated StringPropertyNDB 查询重复的 StringProperty 中的空字符串
【发布时间】:2015-09-15 18:50:26
【问题描述】:

我有具有字符串重复属性的 ndb 模态。我正在尝试检索所有具有空值的实体。但是 NDB 查询返回空。

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

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

【问题讨论】:

    标签: python google-app-engine google-cloud-datastore app-engine-ndb


    【解决方案1】:

    我认为您必须将查询基于另一个包含值数量的字段,例如,

    num_values = ndb.IntegerProperty(indexed=True)
    

    每次更新value 字段时,您都必须更新此号码。然后可以这样查询:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-02
      • 2012-12-26
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 2017-06-12
      • 2014-06-27
      • 2018-11-28
      相关资源
      最近更新 更多