Bridge to enable using Django with Spanner.
Project description
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:
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.
Mac/Linux
pip install virtualenv
virtualenv <your-env>
source <your-env>/bin/activate
<your-env>/bin/pip install python-spanner-django
<your-env>/bin/pip install google-cloud-spanner
Windows
pip install virtualenv
virtualenv <your-env>
<your-env>\Scripts\activate
<your-env>\Scripts\pip.exe install python-spanner-django
<your-env>\Scripts\pip.exe install google-cloud-spanner
Supported versions
At the moment, this library only supports Django 2.2. It also requires Python version 3.6 or later.
This package follows a common versioning convention for Django plugins: the major and minor version components of this package should match the installed version of Django. That is, django-google-spanner~=2.2 works with Django~=2.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
and that’ll take a while to run. After this you should be able to 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 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
Let’s 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
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 it’s own and a full set of limitations are documented over here It is recommended that you go through that list.
Django spanner has a set of limitations as well, please go through the list.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-google-spanner-2.2.1b4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 479f653d3a141d3122a0f94a65a441830b86d97409c272778900f19718e56a25 |
|
MD5 | 6581ec08ee820059bad4f2f019fdefda |
|
BLAKE2b-256 | 424cfa870f71764484a8d4b3facb9690d8d290622ffac035ad9098dae2881bfb |
Hashes for django_google_spanner-2.2.1b4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 544ea1ddf151c3d40d5dcc9d40cdf1b8471ee5d9e45b3dcb51a0c07452dc4c53 |
|
MD5 | c63fac08dba960446c3be3618de444d8 |
|
BLAKE2b-256 | 823e6e5493305c20226427d068250bfdd715d4f12b2a83f6610afe8082d972f9 |