2010-10-13 21 views
3

我有这段代码来查找属性分支为空的所有节点。对于ListProperty中没有值的gqlquery

nobranches=TreeNode.all()

for tree in nobranches:
 if tree.branches==[]:

我想找到一个更好的,更有效的方式来做到这一点。我不需要检索所有TreeNodes的meathod。我试过TreeNode.all()。filter(branches = []),但是这给了我一个消息,“BadValueError('在列表上过滤不被支持'”。我怎么能做一些像TreeNode.gql('WHERE branches = :1',[])。fetch(100)。我试过这个,但是我得到一个“BadValueError:可能不会使用空列表作为属性值;属性是[]”。是否还有其他有效的方法?

BTW,这里是树节点是什么样子

class TreeNode(db.Model):
  name = db.StringProperty()
  branches =db.ListProperty(db.Key)

+0

只是猜测,但是'WHERE branches = None'呢? – sje397 2010-10-13 05:16:38

+0

分支从不是空如果它是空的是一个空列表 – 2010-10-13 05:57:05

回答

0

documentation on how indexes are stored说:

For multi-valued properties, such as ListProperty and StringListProperty, each value has its own index row, so using multi-valued properties does result in more indexing overhead.

因此,对于列表属性中的每个项目,索引中都有一行。

我的期望是,如果有列表中的属性没有项目,那么就会在索引中没有行。因此,使用索引来检索具有空列表的实体是不可能的。

一个解决办法是增加另一个属性(例如hasbranches = db.BooleanProperty()),您保持在您添加或删除分支。然后,您将能够过滤hasbranches = False。

1

你不能用一个过滤器做到这一点:作为萨克森说,没有索引行匹配要检索什么,所以没办法捡回来。

一个简单的替代方法是存储包含列表中元素的数量的另一个属性,并在该过滤器。 aetycoon是一个库,其中包含可能有助于此计算的属性:

class TreeNode(db.Model): 
    name = db.StringProperty() 
    branches = db.ListProperty(db.Key) 
    branch_count = aetycoon.DerivedProperty(lambda self: len(self.branches)) 
相关问题