Question

I'm storing the key to another entity in an ndb.KeyPropery attribute called teacher

class Section(ndb.Model):
    name = ndb.StringProperty(required=True)
    teacher = ndb.KeyProperty(required=True, kind=Staff)
    students = ndb.IntegerProperty(repeated=True)
    active = ndb.BooleanProperty(default=True)

I'm storing things there just fine, but when I retrieve a value it doesn't seem to fully behave like an ndb.Key

(in interactive console on dev server...)

from google.appengine.ext import ndb

import amt
from sections.model import Section

section = Section.query(Section.name == 'test').fetch()[0]

print(section.teacher)
print(type(section.teacher))
print(section.teacher.kind())
print(section.teacher.id())
print(section.teacher.get())
print(ndb.Key(section.teacher.kind(), int(section.teacher.id())).get())

gives...

Key('Staff', '5486563022602240')
<class 'google.appengine.ext.ndb.key.Key'>
Staff
5486563022602240
None
Kxxxxxxx, Mxxx

So why does section.teacher.get() return None and ndb.Key(section.teacher.kind(), int(section.teacher.id())).get() return my entity (__str__ is overridden to just print out a name...)

Was it helpful?

Solution

Notice the difference in the key Key('Staff', '5486563022602240') and your code that fetches ndb.Key(section.teacher.kind(), int(section.teacher.id()) these are different keys.

The first has a key_name defined (str) and your fetching successfully with an id (int).

Maybe you stored the wrong key in teacher the first time around.

How did you construct your original key ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top