I tried to address a repeated property element using a key, like : ndb.Key(‘Books’, ‘Programming.one’) but this key (the .one part) is not valid.
My example model below is a simplified version of my app’s model. In this example code I have dependencies (using keys) between book chapters and tags :
class Tags(ndb.Model):
tag = ndb.StringProperty()
example : Tags(id = ‘python’, tag = ‘python’)
class Books(ndb.Model):
book = ndb.StringProperty)
chapters = ndb.StringProperty(repeated = True)
example : Books(id = ‘Programming’, book = ‘Programming’, chapters = [‘one’, ‘two’])
class Dependencies(ndb.Model):
chapter_key = ndb.KeyProperty()
tag_keys = ndb.KeyProperty(repeated = True)
example :
chapter_key = ndb.Key('Books','Programming.one')
dependency_key = ndb.Key('Dependencies', chapter_key.id())
Dependencies(key = dependency_key, chapter_key = chapter_key,
tag_keys = [ndb.Key('Tags', 'python'), ndb.Key('Tags', 'java')])
Is it possible to address a repeated property using a ndb.Key. In my code example the chapter_key is not valid. Can I use a hook or property subclass to make it work?
To make it work I can combine a valid Book Key with a StringProperty to hold the chapter.
book_key = ndb.Key('Books','Programming')
chapter = 'one'
dependency_key = ndb.Key('Dependencies', book_key.id() + '.' + chapter)
Dependencies(key = dependency_key, book_key = book_key, chapter = chapter,
tag_keys = [ndb.Key('Tags', 'python'), ndb.Key('Tags', 'java')])
But I would like to benifit from a key.
I have the same question for a structured property. For this question the repeated StringProperty chapters is replaced by a repeated StructuredProperty like :
class Chapters(ndb.Model):
chapter = ndb.StringProperty()
word_count = ndb.IntegerProperty()
About my example and the use of keys :
I use keys in Dependencies, because keys in dependencies can refer to different kinds. These kinds differ from the Book like kind, because they do not have a repeated property like Book chapters. I use repeated depends_on_keys in my application, instead of chapter_keys.
In the example I also left out the parent keys. The Book like kind can have dependencies, but in my application you cannot find entities, which depend on the Book like kind.
No, you cannot use a key to identify a part of an entity. If you want to reference a part of an entity, you will need to use a key in conjunction with your own scheme for addressing entity parameters.