django

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
/usr/local/google_appengine

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

$GAE/remote_api_shell.py -s blhaastest.appspot.com

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.
s~blhaastest>

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 manage.py.

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

> import library.models

In the example above, the model definitions are in a file named models.py, 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!

> Read More
Category:

Installing Python and Django using MacPorts

Use MacPorts to install python (I'm using Python version 2.7 for this demo):
$ sudo port install python27

If successful, MacPorts should give you this informational message: "To make python 2.7 the default (i.e. the version you get when you run 'python'), please run: sudo port select --set python python27" So let's do that:
$ sudo port select --set python python27

If you want to make sure that installation worked, and you have the correct version, check like this:
$ which python
/opt/local/bin/python
 
$ python --version
Python 2.7.8

Using your MySQL server command line, create a MySQL database. I'm using the name 'djangodemo' for this example:
> CREATE DATABASE djangodemo

Install pip:
$ sudo port install py27-pip

If successful, MacPorts should give you this informational message: "To make the Python 2.7 version of pip the one that is run when you execute the commands without a version suffix, e.g. 'pip', run: port select --set pip pip27" Let's do that:
$ sudo port select --set pip pip27

Now we can install Django:
$ sudo pip install Django

Verify that Django is installed:
$ python -c "import django; print(django.get_version())"
1.6.6

Add the Python bin directory to your path. Add this line to your .bash_profile:
export PATH="$PATH:/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin"

If you're using MySQL, install the Python/MySQL connector:
sudo port install py-mysql

Now you can start building your project. The official Django tutorial is a great place to start.

> Read More
Category:
Syndicate content