문제

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)

도움이 되었습니까?

해결책

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.

다른 팁

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))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top