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.)
- Install django-spanner
- Ensure you have a Cloud Spanner database already created
- Follow the tutorial
- Update your settings.py file to use django-spanner
- Set credentials and project environment variables
- Apply the migrations
- Now run your server
- Create an Django admin user
- Login as admin
- Comprehensive hands-on guide
- References
To install from PyPI:
pip3 install django-google-spanner
To install from source:
git clone https://github.com/googleapis/python-spanner-django
cd python-spanner-django/
pip3 install .
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.
Please follow the guides in https://docs.djangoproject.com/en/2.2/intro/tutorial01/ until the end with a single DISTINCTION:
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/settings.py file to the following:
a) add django_spanner
as the very first entry to your INSTALLED_APPS
INSTALLED_APPS = [
'django_spanner', # Must be listed first.
...
]
b) update DATABASES
into the following:
DATABASES = {
'default': {
'ENGINE': 'django_spanner',
'PROJECT': PROJECT_ID,
'INSTANCE': SPANNER_INSTANCE,
'NAME': SPANNER_DATABASE_NAME,
}
}
and for example here is a filled in database where:
PROJECT_ID
: spanner-appdevINSTANCE
: instanceNAME
: db1
which when filled out, will look like this
DATABASES = {
'default': {
'ENGINE': 'django_spanner',
'PROJECT': 'spanner-appdev',
'INSTANCE': 'instance',
'NAME': 'db1',
}
}
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
Please run:
$ python3 manage.py migrate
and that'll take a while running, but when done, it will look like the following
$ python3 manage.py 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
After those migrations are completed, that will be all. Please continue on with the guides.
First you’ll need to create a user who can login to the admin site. Run the following command:
$ python3 manage.py createsuperuser
which will then produce a prompt which will allow you to create your super user
Username: admin
Email address: admin@example.com
Password: **********
Password (again): **********
Superuser created successfully.
Let’s run the server
python3 manage.py runserver
Then visit http://127.0.0.1:8000/admin/
For a more comprehensive, step by step hands-on guide, please visit using django-spanner from scratch
Resource | URL |
---|---|
Cloud Spanner homepage | https://cloud.google.com/spanner/ |
django-spanner project's source code | https://github.com/googleapis/python-spanner-django/ |
django-spanner from scratch | https://orijtech-161805.firebaseapp.com/quickstart/new_app/ |