Using the Google App Engine Datastore Remote API with Django

So you have a Django application and you want to inspect the data in your production Google App Engine (GAE) datastore. What are your options? You could use the developers console - it has a query tool - but it's pretty limited, and it doesn't currently provide a way of exporting elements. You could write and deploy a script that collects and prints the data, but who wants to do a deployment every time you want to tweak the script? So what can you do?

Fortunately GAE offers a Remote API for Python for interacting with remote data elements from the comfort of your very own CLI.

For this example, I'll be using a project with the id blhaastest. If you're unsure of your project id, you can find it in your developer console:

First thing to do is verify your Google App Engine symlinks resolves correctly:

> echo $GAE

Next, start the Remote API Shell (Google login required; may require an App Password if 2-factor-authentication is enabled):

$GAE/ -s

If your login is successful, you should get a response that looks like this:

App Engine remote_api shell
Python 2.7.9 (default, Dec 11 2014, 02:36:08)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)]
The db, ndb, users, urlfetch, and memcache modules are imported.

Now that your remote shell is up and running, you can begin to execute commands against your remote datastore.

First thing to do is import your Django settings, so the shell can begin to know about your application:

> import os
> os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

Experienced Django developers will recognize this line above as having been plucked from

Next, import the file containing your model definition(s):

> import library.models

In the example above, the model definitions are in a file named, located in the library directory.

You can now begin using QuerySet operations. For example, here's how you can fetch all Books, sorted by title:

> books = library.models.Book.objects.all().order_by('title')

You can print the Book titles like this (Remote API Shell allows multi-line input, terminated by an empty line):

> for book in books:
>     print book.title
> [return]

Results in the following output:

Don Quixote
Invisible Man
Nineteen Eighty Four
The Illustrated Man
Things Fall Apart

Special thanks to GAE documentation for this article, which I used to help get me started. It has more tips and tricks for using the Remote API, so check it out!



Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

This question is for testing whether you are a human visitor and to prevent automated spam submissions.