Skip to main content

Bridge to enable using Django with Spanner.

Project description

GA pypi versions

Cloud Spanner is the world’s first fully managed relational database service to offer both strong consistency and horizontal scalability for mission-critical online transaction processing (OLTP) applications. With Cloud Spanner you enjoy all the traditional benefits of a relational database; but unlike any other relational database service, Cloud Spanner scales horizontally to hundreds or thousands of servers to handle the biggest transactional workloads.

Quick Start

In order to use this library, you first need to go through the following steps:

  1. Select or create a Cloud Platform project.

  2. Enable billing for your project.

  3. Enable the Google Cloud Spanner API.

  4. Setup Authentication.

This package provides a 3rd-party database backend for using Cloud Spanner with the Django ORM. It uses the Cloud Spanner Python client library under the hood.

Installation

Install this library in a virtualenv using pip. virtualenv is a tool to create isolated Python and Django environments. The basic problem it addresses is one of dependencies and versions, and indirectly permissions.

With virtualenv, it’s possible to install this library without needing system install permissions, and without clashing with the installed system dependencies.

Supported versions

The library supports Django 2.2, and Django 3.2. Both versions are long-term support (LTS) releases for the Django project<https://www.djangoproject.com/download/#supported-versions>_. The minimum required Python version is 3.6.

pip3 install django==3.2

Installing the package

To install from PyPI:

pip3 install django-google-spanner

To install from source:

git clone git@github.com:googleapis/python-spanner-django.git
cd python-spanner-django
pip3 install -e .

Creating a Cloud Spanner instance and database

If you don’t already have a Cloud Spanner database, or want to start from scratch for a new Django application, you can create a new instance and database using the Google Cloud SDK:

gcloud spanner instances create $INSTANCE --config=regional-us-central1 --description="New Django Instance" --nodes=1
gcloud spanner databases create $DB --instance $INSTANCE

Configuring settings.py

This package provides a Django application named django_spanner. To use the Cloud Spanner database backend, the application needs to installed and configured:

  • Add django_spanner as the first entry in INSTALLED_APPS:

    INSTALLED_APPS = [
        'django_spanner',
        ...
    ]
  • Edit the DATABASES setting to point to an existing Cloud Spanner database:

    DATABASES = {
        'default': {
            'ENGINE': 'django_spanner',
            'PROJECT': '$PROJECT',
            'INSTANCE': '$INSTANCE',
            'NAME': '$DATABASE',
        }
    }

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

That’ll take a while to run. After this you should be able to see the tables and indexes created in your Cloud Spanner console.

Create a Django admin user

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.

Login as admin

Now, run the server

python3 manage.py runserver

Then visit http://127.0.0.1:8000/admin/

Create and register your first model

Please follow the guides in https://docs.djangoproject.com/en/2.2/intro/tutorial02/#creating-models to create and register the model to the Django’s automatically-generated admin site.

How it works

Overall design

"Overall Design"

Internals

"Internals"

Executing a query

Here is an example of how to add a row for Model Author, save it and later query it using Django

>>> author_kent = Author( first_name="Arthur", last_name="Kent", rating=Decimal("4.1"),)
>>> author_kent.save()
>>> qs1 = Author.objects.all().values("first_name", "last_name")

How to contribute

Contributions to this library are always welcome and highly encouraged.

See CONTRIBUTING for more information on how to get started.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See the Code of Conduct for more information.

Limitations

Spanner has certain limitations of its own. The full set of limitations is documented here. It is recommended that you go through that list.

Django spanner has a set of limitations as well, which you can find here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-google-spanner-3.0.0.tar.gz (67.8 kB view details)

Uploaded Source

Built Distribution

django_google_spanner-3.0.0-py3-none-any.whl (85.0 kB view details)

Uploaded Python 3

File details

Details for the file django-google-spanner-3.0.0.tar.gz.

File metadata

  • Download URL: django-google-spanner-3.0.0.tar.gz
  • Upload date:
  • Size: 67.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for django-google-spanner-3.0.0.tar.gz
Algorithm Hash digest
SHA256 cd1ff51745c240c388f472e660f0b3db4c3444567b2f22daa9564dea03cdce41
MD5 fd5a6a80835eda20c5f57a67c2068b95
BLAKE2b-256 9eea7ccbf9292c6df3a15cd4d5fac7c0e55bdf21091a76ee2e4958bd200d70ab

See more details on using hashes here.

File details

Details for the file django_google_spanner-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: django_google_spanner-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 85.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for django_google_spanner-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 22702e6c4cd1d1bb64d75658685ed0cd1da69d34d90afbc8be42fd99108fe82b
MD5 d9b5c51c3ec792b4c23f097f1f0dfc5f
BLAKE2b-256 7eefbc8b4c9653409be3b1b0ae6eb17bff23960da46a81dff87cf9790ffb0acd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page