I looked through the documentation, documents and answers to questions and answers, and I'm still struggling with understanding a small part of this. What to choose and when?
This is what I read so far (just a sample):
The key class seems pretty simple to me. When you create an ndb object, the data warehouse automatically creates a key for you, usually in the form of a key (Kind, id), where an identifier is created for you.
Let's say you have these two models:
class Blah(ndb.Model): last_name = ndb.StringProperty() class Blah2(ndb.Model): first_name = ndb.StringProperty() blahkey = ndb.KeyProperty()
So, just using the key, and you want to make Blah1 a parent (or have several family members with the same name)
lname = Blah(last_name = "Bonaparte") l_key = lname.put() **OR** l_key = lname.key.id()
then
lname = Blah2( parent=fname_key, first_name = "Napoleon") lname.put() lname2 = Blah2( parent=fname_key, first_name = "Lucien") lname2.put()
So far so good (I think). Now about KeyProperty for Blah2. Suppose Blah1 is still the same.
lname3 = Blah2( first_name = "Louis", blahkey = fname_key) lname3.put()
Is it correct?
How to request various things
Request Name:
Blah.query() # all last names Blah.query(last_name='Bonaparte') # That specific entity.
Name:
Blah2.query() napol = Blah2.query(first_name = "Napoleon") bonakey = napol.key.parent().get() # returns Bonaparte key ?? bona = bonakey.get() # I think this might be redundant
that's where i get lost. How to search for Bonaparte from the first name using a key or key property. I did not add it here and may have had it, and this is a discussion of parents, great parents, great parents, since Case keeps track of ancestors / parents.
How and why you will use KeyProperty against the inherent key class. Also imagine that you have 3 sensors s1, s2, s3. Each sensor had thousands of readings, but you want to keep the readings associated with s1 so that you can graphically read all the readings for today for s1. What would you use? KeyProperty or a key class? We apologize if this was answered elsewhere, but I have not seen a clear example / guide on choosing what and why / how.