开发者

gqlquery for no values in ListProperty

开发者 https://www.devze.com 2023-01-20 05:21 出处:网络
I have this code to find all the nodes where property branches is empty. nobranches=TreeNode.all() for tree in nobranches:

I have this code to find all the nodes where property branches is empty.

nobranches=TreeNode.all()

for tree in nobranches:

 if tree.branches==[开发者_如何学编程]:

I wanted to find a better, more efficient way to do this. A meathod where I don't have to retrieve all the TreeNodes. I have tried TreeNode.all().filter(branches=[]) but this gives me a message, "BadValueError('Filtering on lists is not supported'" . How can I do something like TreeNode.gql('WHERE branches=:1', []).fetch(100). I tried this, but I get a "BadValueError: May not use the empty list as a property value; property is []". Is there any other efficient way?

BTW, Here is what TreeNode looks Like

class TreeNode(db.Model):

  name = db.StringProperty()

  branches =db.ListProperty(db.Key)


You can't do this with a filter: as Saxon says, there's no index row matching what you want to retrieve, and so no way to retrieve it.

One simple alternative is to store another property that contains the number of elements in the list, and filter on that. aetycoon is a library that contains computed properties that may help with that:

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


The documentation on how indexes are stored says:

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.

So for each item in your list property, there is a row in the index.

My expectation would be that if there are no items in the list property, then there would be no rows in the index. So it wouldn't be possible to use the index to retrieve entities with an empty list.

One solution would be to add another property (eg hasbranches = db.BooleanProperty()), which you maintain when you add or remove branches. Then you will be able to filter for hasbranches = False.

0

精彩评论

暂无评论...
验证码 换一张
取 消