Created
November 22, 2017 18:56
-
-
Save stephen-soltesz/0c5c888002183dae228a8a7a6f9175ef to your computer and use it in GitHub Desktop.
datastore python example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Imports the Google Cloud client library | |
from google.cloud import datastore | |
# Instantiates a client | |
datastore_client = datastore.Client('mlab-staging') | |
# The kind for the new entity | |
kind = 'Task' | |
# The name/ID for the new entity | |
name = 'sampletask1' | |
# The Cloud Datastore key for the new entity | |
task_key = datastore_client.key(kind, name) | |
# Prepares the new entity | |
task = datastore.Entity(key=task_key) | |
task['description'] = 'Buy milk' | |
# Saves the entity | |
datastore_client.put(task) | |
print('Saved {}: {}'.format(task.key.name, task['description'])) | |
key = datastore_client.key('Task') | |
ids = datastore_client.allocate_ids(key, 1) | |
task = datastore.Entity(key=ids[0]) | |
task['description'] = 'Works from python' | |
datastore_client.put(task) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment