Reputation: 273
In this page: https://developers.google.com/appengine/docs/python/datastore/keyclass#Key_from_path
This part:
Key.from_path(kind, id_or_name, parent=none, namespace=None, **kwds) Builds a new Key object from an ancestor path of one or more entity keys.
A path represents the hierarchy of parent-child relationships for an entity. Each entity in the path is represented the entity's kind, and either its numeric ID or its key name. The full path represents the entity that appears last in the path, with its ancestors (parents) as preceding entities.
For example, the following call creates a key for an entity of kind Address with the numeric ID 9876 whose parent is an entity of kind User with the named key 'Boris':
k = Key.from_path('User', 'Boris', 'Address', 9876)
For more information about paths, see Keys and Entity Groups.
The function call and the explaination just don't make sense here, if Address is the "kind" argument, should it go first?? And the ID 9876 should go second? Why are they 3rd and 4th?
And "parent" is the third parameter, why are there two "parent" arguments (kind 'User' and name 'Boris') here and they are first and second in the argument list?
Upvotes: 4
Views: 1258
Reputation: 74094
From the Gae source code, the signature of the from_path
static method of the Key
class is:
def from_path(*args, **kwds):
accepting a non-zero even number of positional arguments in the form of (kind, id or name, kind, id or name, etc. etc.)
:
if not args or len(args) % 2:
raise datastore_errors.BadArgumentError(
'A non-zero even number of positional arguments is required '
'(kind, id or name, kind, id or name, ...); received %s' % repr(args))
As stated in the documentation, the full path represents the entity that appears last in the path, with its ancestors (parents) as preceding entities.
So the example seems correct; the created key is the key of the entity of kind Address with id
9876 having a parent of kind User with 'Boris' as key name
.
Upvotes: 2
Reputation: 31928
Keys in AppEngine are hierarchical, to get a full key you need to pass all the ancestry information.
In the case of the example there are two objects types: User and Address, User is a parent of to Address. The call to from_path first provides object type User which his id is Boris (id can be a string name or integer id), this object has a child object of type Address which his id is 9876.
Upvotes: 2