views:

40

answers:

2

I'm trying to find all object which have no parent (i.e. which were created with parent=None). Using M.all().filter("parent = ", None).fetch(100) doesn't bring any results, even though some objects certainly do have no parent.

What am I doing wrong?

A: 

You don't use filter() to query for an ancestor. Try instead:

M.all().ancestor(None).fetch(100)

Edit: hmm, that won't work apparently (I'd swear I had done this somewhere). So you'd need to save an extra property as a flag for root entities.

moraes
I don't want to look for ancestor (which I understand is the parent or the parent's parent or the parent's parent's parent etc). Instead, I want to query for the immediate parent.
Martin v. Löwis
When your create an entity with parent=None, it is an entity with no ancestor. You don't query for it using .filter('parent', ...), but using .ancestor().To be able to query for immediate parent, you need to store an extra property for the parent and query on that.
moraes
+1  A: 

There's no way to query specifically for root entities. You need to either use external information (eg, no entity of type Foo has parents), or add a property that indicates if an entity is a root entity or not.

Nick Johnson
That's sad. I wonder what the point then is for having a hierarchy of objects at all.
Martin v. Löwis
So you can quickly find descendent entities, and look up the parent of an entity, and to provide transactional domains.
Nick Johnson