django-spanner for Django tutorial

This example shows how to use django-spanner for Cloud Spanner as a backend database for Django’s tutorials

NOTE: Use the version of python-spanner-django that corresponds to your version of Django. For example, python-spanner-django 2.2.x works with Django 2.2.y. (This is the only supported version at this time.)

Table of contents

Install django-spanner

To install from PyPI:

pip3 install django-google-spanner

To install from source:

git clone
cd python-spanner-django/
pip3 install .

Ensure you have a Cloud Spanner database already created

If you haven’t already, please follow the steps to install Cloud Spanner, or visit this codelab

You’ll need to ensure that your Google Application Default Credentials are properly downloaded and saved in your environment.

Follow the tutorial

Please follow the guides in until the end with a single DISTINCTION:

Update your file to use django-spanner

After we have a Cloud Spanner database created, we’ll need a few variables:

  • ProjectID

  • Instance name

  • Database name aka DisplayName

Once in, edit the DATABASES section of your mysite/ file to the following:

a) add django_spanner as the very first entry to your INSTALLED_APPS

    'django_spanner',  # Must be listed first.

b) update DATABASES into the following:

    'default': {
        'ENGINE': 'django_spanner',

and for example here is a filled in database where:

  • PROJECT_ID: spanner-appdev

  • INSTANCE: instance

  • NAME: db1

which when filled out, will look like this

    'default': {
        'ENGINE': 'django_spanner',
        'PROJECT': 'spanner-appdev',
        'INSTANCE': 'instance',
        'NAME': 'db1',

Set credentials and project environment variables

You’ll need to download a service account JSON key file and point to it using an environment variable:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json
export GOOGLE_CLOUD_PROJECT=gcloud_project

Apply the migrations

Please run:

$ python3 migrate

and that’ll take a while running, but when done, it will look like the following

$ python3 migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

After this you should can see the tables and indices created in your Cloud Spanner console

Now run your server

After those migrations are completed, that will be all. Please continue on with the guides.

Create an Django admin user

First you’ll need to create a user who can login to the admin site. Run the following command:

$ python3 createsuperuser

which will then produce a prompt which will allow you to create your super user

Username: admin
Email address:
Password: **********
Password (again): **********
Superuser created successfully.

Login as admin

Let’s run the server

python3 runserver

Then visit

Create and register your first model

Please follow the guides in to create and register the model to the Django’s automatically-generated admin site.


Resource|URL —|— Cloud Spanner homepage| django-spanner project’s source code|