[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-20。"],[[["\u003cp\u003eEntities in Datastore are categorized under a specific "kind" for querying, and each entity's data is stored as properties.\u003c/p\u003e\n"],["\u003cp\u003eEntities can be created and their properties set either through the constructor, manual setting, or the \u003ccode\u003epopulate()\u003c/code\u003e method, with type checking enforced for properties.\u003c/p\u003e\n"],["\u003cp\u003eEntities are stored in the Datastore by calling \u003ccode\u003eput()\u003c/code\u003e, which will return a key for later retrieval of the entity.\u003c/p\u003e\n"],["\u003cp\u003eYou can retrieve an entity using its key with the \u003ccode\u003eget()\u003c/code\u003e method, and you can also encode the key into a URL-safe string for later reconstruction.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003endb\u003c/code\u003e library allows for batch operations on entities and keys, significantly reducing remote procedure calls to the Datastore, leading to greater efficiency.\u003c/p\u003e\n"]]],[],null,["# Creating, Retrieving, Updating, and Deleting Entities\n\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| python3\n|\n| /services/access). If you are updating to the App Engine Python 3 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/python-differences) to learn about your migration options for legacy bundled services.\n\nData objects in Datastore are known as *entities* , each of which is categorized\nunder a particular *kind* for the purpose of queries. For\ninstance, if you are writing a human resources application you might represent\neach employee with an entity of kind `Employee`. Note that the entity data\nvalues are in the form of\n\n[properties](/appengine/docs/legacy/standard/python/ndb/entity-property-reference#properties_and_value_types).\n\n\nFor more information about entities, see the documentation on\n[ancestor paths](/appengine/docs/legacy/standard/python/ndb/creating-entity-keys#using_the_ancestor_path_in_the_key) and [transactions](/appengine/docs/legacy/standard/python/ndb/transactions).\n| **Important:** Inefficient data store code can result in unnecessarily high costs! So before you write your code, you should learn about [write costs](/datastore/pricing).\n\nCreating entities and setting properties\n----------------------------------------\n\nYou create an entity and set by calling the constructor method for its model\nclass. See [Creating and Using Entity Model Classes](/appengine/docs/legacy/standard/python/ndb/creating-entity-models)\nfor information on creating an entity model class.\n\nThe following example shows how to invoke a model class constructor with\nkeyword arguments: \n\n sandy = Account(\n username='Sandy', userid=123, email='sandy@example.com')\n\nThis code creates an object in your program's main memory. However, note that\nthe entity disappears when the process terminates, so you must also persist the\nentity to Datastore, by calling `put()`, as follows: \n\n sandy_key = sandy.put()\n\nNotice that this returns a key that you can use for\n[retrieving the entity](#retrieving_entities) from Datastore\nlater.\n\nSet properties by using one of the following options:\n\n- Specify the entity's properties to the constructor with keyword arguments: \n\n sandy = Account(\n username='Sandy', userid=123, email='sandy@example.com')\n\n- Set properties manually after entity creation: \n\n sandy = Account()\n sandy.username = 'Sandy'\n sandy.userid = 123\n sandy.email = 'sandy@example.com'\n\n- Use the `populate()` convenience method to set several properties in one operation: \n\n sandy = Account()\n sandy.populate(\n username='Sandy',\n userid=123,\n email='sandy@gmail.com')\n\nHowever you choose to set the entity's properties, the property types (in this\ncase, `StringProperty` and `IntegerProperty`) enforce type checking.\n\nFor example: \n\n bad = Account(\n username='Sandy', userid='not integer') # raises an exception\n\n`...` \n\n sandy.username = 42 # raises an exception\n\n\u003cbr /\u003e\n\nRetrieving Entities from Keys\n-----------------------------\n\nIf you have an entity's key, you can retrieve the entity from\nDatastore: \n\n sandy = sandy_key.get()\n\nThe Key methods `kind()` and `id()` recover the entity's kind and identifier\nfrom the key: \n\n kind_string = sandy_key.kind() # returns 'Account'\n ident = sandy_key.id() # returns '2'\n\nYou can also use an entity's key to obtain an encoded string suitable for\nembedding in a URL: \n\n url_string = sandy_key.urlsafe()\n\nThis produces a result like `agVoZWxsb3IPCxIHQWNjb3VudBiZiwIM` which can later\nbe used to reconstruct the key and retrieve the original entity: \n\n sandy_key = ndb.Key(urlsafe=url_string)\n sandy = sandy_key.get()\n\nNotice that the URL-safe string looks cryptic, but it is not encrypted! It can easily\nbe decoded to recover the original entity's kind and identifier: \n\n```python\nkey = Key(urlsafe=url_string)\nkind_string = key.kind()\nident = key.id()\n```\n\nIf you use such URL-safe keys, don't use sensitive data such as\nemail addresses as entity identifiers. A possible solution would\nbe to use a hash of the sensitive data as the identifier.\nThis stops third parties, who can see the encrypted keys, from using\nthem to harvest email addresses, though it doesn't stop them from\nindependently generating their own hash of a known email address and\nusing it to check whether that address is present in Datastore.\n\nUpdating entities\n-----------------\n\nTo update an existing entity, retrieve it from Datastore,\nmodify its properties, and store it back again: \n\n sandy = key.get()\n sandy.email = 'sandy@example.co.uk'\n sandy.put()\n\nYou can ignore the value returned by `put()` in this case, since an entity key\ndoesn't change when you update it.\n\nDeleting entities\n-----------------\n\nWhen an entity is no longer needed, you can remove it from\nDatastore with the key's `delete()` method: \n\n sandy.key.delete()\n\nNote that this is an operation on the key, not on the entity itself. It always\nreturns `None`.\n\nDeleting entities in bulk\n-------------------------\n\nIf you need to delete a large number of entities, we recommend [using\nDataflow to delete entities in bulk](/datastore/docs/bulk-delete).\n\nUsing batch operations\n----------------------\n\n| **Note:** The `ndb` library automatically batches most calls to Datastore, so in most cases you don't need to use the explicit batching operations shown below.\n\nYou can process a collection of entities or keys in a single call rather than\nindividually in separate calls, for example inside a loop. This results in\na single remote procedure call (RPC) for the batch, rather than a separate RPC\ncall for each entity.\n\nThe following code shows how you do this: \n\n list_of_keys = ndb.put_multi(list_of_entities)\n list_of_entities = ndb.get_multi(list_of_keys)\n ndb.delete_multi(list_of_keys)\n\n| **Note:** The methods shown above interact correctly with the [context and caching](/appengine/docs/legacy/standard/python/ndb/cache); they don't correspond directly to specific RPC calls.\n\nIn the above code, you pass a list of key objects to `ndb.get_multi` to fetch\nmultiple entities in a batch; `ndb.get_multi` returns a list of entity objects,\nwith `None` values for keys that do not have a corresponding entity in\nDatastore. Getting the entities in this way results in\nfewer calls to Datastore for the entire batch. (The number of\ncalls per batch depends on your batch size settings.)"]]