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.
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.
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.
The library supports Django 3.2, and Django 4.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.
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
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', } }
The Spanner Django engine by default uses random int64 values that are generated by the client as primary key values. This default is applied to all databases that are configured, including databases that use a different engine than Spanner. You can disable this behavior with the RANDOM_ID_GENERATION_ENABLED setting:
Transaction support in autocommit modeDATABASES = { 'default': { 'ENGINE': 'django_spanner', 'PROJECT': '$PROJECT', 'INSTANCE': '$INSTANCE', 'NAME': '$DATABASE', 'RANDOM_ID_GENERATION_ENABLED': false, } }
Django version 4.2 and higher by default supports transactions in autocommit mode. A transaction is automatically started if you define an [atomic block](https://docs.djangoproject.com/en/4.2/topics/db/transactions/#controlling-transactions-explicitly).
Django version 3.2 and earlier did not support transactions in autocommit mode with Spanner. You can enable transactions in autocommit mode with Spanner with the ALLOW_TRANSACTIONS_IN_AUTO_COMMIT configuration option.
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
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 userFirst 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.
Now, run the server
python3 manage.py runserver
Then visit http://127.0.0.1:8000/admin/
Create and register your first modelPlease follow the guides in https://docs.djangoproject.com/en/4.2/intro/tutorial02/#creating-models to create and register the model to the Django’s automatically-generated admin site.
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")
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.
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.
Features from spanner that are not supported in Django-spanner are listed here.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4