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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With