Stay organized with collections Save and categorize content based on your preferences.
This API is supported for first-generation runtimes and can be used when upgrading to corresponding second-generation runtimes. If you are updating to the App Engine Python 3 runtime, refer to the migration guide to learn about your migration options for legacy bundled services.Each entity is identified by a key that is unique within the application's Datastore instance, and consists of the following:
_get_kind()
.The following example implicitly creates a key with a string identifier using the named parameter id
:
You could alternatively set the key name directly:
Letting Datastore generate an ID to use for the keyThis code shows how to use an auto-generated ID as the key:
Using the ancestor path in the keyThe sequence of entities beginning with a root entity and proceeding from parent to child, leading to a given entity, constitute that entity's ancestor path. An entity, its parent, parent's parent, and so on recursively, are the entity's ancestors. The entities in Datastore form a hierarchical key space similar to the hierarchical directory structure of a file system.
The complete key identifying an entity consists of a sequence of kind-identifier pairs specifying its ancestor path and terminating with those of the entity itself. The constructor method for class Key
accepts such a sequence of kinds and identifiers and returns an object representing the key for the corresponding entity.
The following example shows a blogging service that stores messages by revision. Messages are organized under accounts, and revisions are under messages.
...
In the sample, ('Account', 'sandy@example.com')
, ('Message', 123)
, and ('Revision', '1')
are all examples of kind-identifier pairs.
Notice that Message
is not a model class; it is used only as a way to group revisions, not to store data.
As shown in the sample code, the entity's kind is designated by the last kind-name pair in the list: ndb.Key('Revision', '1')
.
You can use the named parameter parent
to designate any entity in the ancestor path directly. All of the following notations represent the same key:
For a root entity, the ancestor path is empty and the key consist solely of the entity's own kind and identifier.
Specifying an entity with ancestorsTo insert a new message with parent keys
For keys that were created with a parent, the parent()
method returns a key representing the parent entity:
You can create an entity without specifying an ID, in which case the data store automatically generates a numeric ID. If you choose to specify some IDs and then let Datastore automatically generate some IDs, you could violate the requirement for unique keys. To avoid this, reserve a range of numbers to use to choose IDs or use string IDs to avoid this issue entirely.
To reserve a range of IDs, use the model class' allocate_ids()
class method:
To allocate 100 IDs for a given model class MyModel
:
To allocate 100 IDs for entities with parent key p
:
The returned values, first
and last
, are the first and last IDs (inclusive) that are allocated. You can use these to construct keys as follows:
These keys are guaranteed not to have been returned previously by the data store's internal ID generator, nor will they be returned by future calls to the internal ID generator. However, the allocate_ids()
method does not check whether the IDs returned are present in the data store; it only interacts with the ID generator.
To allocate all IDs up to a given maximum value:
This form ensures that all IDs less than or equal to N
are considered allocated. The return values, first
and last
, indicate the range of IDs reserved by this operation. It is not an error to attempt to reserve IDs already allocated; if that happens, first
indicates the first ID not yet allocated and last
is the last ID allocated.
allocate_ids()
in a transaction.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-07-18 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-07-18 UTC."],[[["Entities in the Datastore are uniquely identified by a key, which includes a kind and an identifier, either specified by the user or automatically generated by Datastore."],["The key identifier can be a string, set by the user, or an integer ID, automatically generated by Datastore, which can be chosen by either setting an `id` parameter or letting it be generated upon using `put`."],["Entities can have an ancestor path, forming a hierarchical structure similar to a file system, where the complete key includes a sequence of kind-identifier pairs representing the entity and its ancestors."],["The `allocate_ids()` method can be used to reserve a range of numeric IDs for a specific model, ensuring unique key creation, and this can be done either by specifying a number of ID's or a max number."],["The `parent` named parameter can be used to designate any entity in the ancestor path directly, offering flexibility in defining entity relationships and key structures."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4