Reputation: 844
I'm still trying to get my head around how the datastore works. I don't have previous experience with databases, so it's not a conflicting paradigm situation; I think I'm just confused about NDB's ancestor structure.
Let's say I have this model class:
class Spam(Model.ndb)
eggs = ndb.StringProperty();
So I create an instance and store it like this:
foo = Spam(eggs="some string")
foo.put()
I understand that put()
returns a key that I could easily call get()
on if I'm trying to access it from the same script, but is there a way to specify my own key, so I can easily access the foo
entity from another script in my app?
I realize I can specify a parent for foo
like this:
foo = Spam(parent=ndb.Key("Bar","Baz"),eggs="some string")
But from there, how would I use "Bar"
and/or "Baz"
to access foo
in a different script?
Upvotes: 0
Views: 96
Reputation: 2542
Parents are used if you have a hierarchy. So if you have recipe books you would put the book as the parent and each recipe as a child. I don't think that's what you want.
If you want to set the key do this:
SuperEggs= Spam(id='SuperEggs', eggs="2 egg whites")
SuperEggs.put()
You can always let App engine set its own keys, this will prevent contention and accidental over rides, when you want access to the entity again simply do a get on some field. Add a name and search that.
FYI the returned id from the put lets you access from any part of your app (or any authorized app). The datastore is global not specific to a script.
Upvotes: 1