Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NDB: How to use get_by_id for entities in an entity group

I'm using get_by_id() to read entities from NDB and I do not get it to work for entities that are part of an entity group:

from google.appengine.ext import ndb

class Folder(ndb.Model):
    name = ndb.StringProperty()

parent_folder_key = Folder(name = 'Parent folder').put()    
sub_folder_key = Folder(name = 'Subfolder', parent=parent_folder_key).put() 

id_list = []
print 'All folders:'
for f in Folder.query():
  print f
  id_list.append(f.key.id())

print '\nFolders by id:'
for id in id_list:
  print Folder.get_by_id(id)

Output:

All folders:
Folder(key=Key('Folder', 5814), name=u'Parent folder')
Folder(key=Key('Folder', 5814, 'Folder', 5815), name=u'Subfolder')

Folders by id:
Folder(key=Key('Folder', 5814), name=u'Parent folder')
None

Is by design or is it a bug? I saw that there have been some issues relating to get_by_id() when using namespaces previously (I'm using SDK 1.6.6). How can I folders that have parents from the id?

like image 290
Arne S Avatar asked Feb 21 '23 03:02

Arne S


1 Answers

get_by_id(id, parent=None) takes a parent parameter.
when you query by id in an entity group you have to include the parent key to be able to get the entity you want.

https://developers.google.com/appengine/docs/python/ndb/modelclass#Model_get_by_id

like image 123
aschmid00 Avatar answered Feb 25 '23 16:02

aschmid00