Package: django-google-spanner
Cloud Spanneris 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.Client Library DocumentationProduct DocumentationQuick StartIn order to use this library, you first need to go through the following steps:Select or create a Cloud Platform project.Enable billing for your project.Enable the Google Cloud Spanner API.Setup Authentication.This package provides a3rd-party database backendfor usingCloud Spannerwith theDjango
ORM. It uses theCloud
Spanner Python client libraryunder the hood.InstallationInstall this library in avirtualenvusing pip.virtualenvis a tool to
create isolated Python and Django environments. The basic problem it addresses is one of
dependencies and versions, and indirectly permissions.Withvirtualenv, it’s possible to install this library without needing system
install permissions, and without clashing with the installed system
dependencies.Supported versionsThe library supportsDjango 2.2, andDjango 3.2.
Both versions are long-term support (LTS) releases for theDjango project<https://www.djangoproject.com/download/#supported-versions>_.
The minimum required Python version is 3.6.pip3installdjango==3.2Installing the packageTo install from PyPI:pip3installdjango-google-spannerTo install from source:gitclonegit@github.com:googleapis/python-spanner-django.gitcdpython-spanner-djangopip3install-e.Creating a Cloud Spanner instance and databaseIf you don’t already have a Cloud Spanner database, or want to start from
scratch for a new Django application, you cancreate a new instanceanddatabaseusing the Google Cloud SDK:gcloudspannerinstancescreate$INSTANCE--config=regional-us-central1--description="New Django Instance"--nodes=1gcloudspannerdatabasescreate$DB--instance$INSTANCEConfiguringsettings.pyThis package provides a Django application nameddjango_spanner. To use the
Cloud Spanner database backend, the application needs to installed and
configured:Adddjango_spanneras the first entry inINSTALLED_APPS:INSTALLED_APPS=['django_spanner',...]Edit theDATABASESsetting to point to an existing Cloud Spanner database:DATABASES={'default':{'ENGINE':'django_spanner','PROJECT':'$PROJECT','INSTANCE':'$INSTANCE','NAME':'$DATABASE',}}Set credentials and project environment variablesYou’ll need to download a service account JSON key file and point to it using an environment variable:exportGOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.jsonexportGOOGLE_CLOUD_PROJECT=gcloud_projectApply the migrationsPlease run:$python3manage.pymigrateThat’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:$python3manage.pycreatesuperuserwhich will then produce a prompt which will allow you to create your super userUsername:adminEmailaddress:admin@example.comPassword:**********Password(again):**********Superusercreatedsuccessfully.Login as adminNow, run the serverpython3manage.pyrunserverThen visithttp://127.0.0.1:8000/admin/Create and register your first modelPlease follow the guides inhttps://docs.djangoproject.com/en/2.2/intro/tutorial02/#creating-modelsto create and register the model to the Django’s automatically-generated admin site.How it worksOverall designInternalsExecuting a queryHere 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 contributeContributions to this library are always welcome and highly encouraged.SeeCONTRIBUTINGfor 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 theCode
of Conductfor more information.LimitationsSpanner has certain limitations of its own. The full set of limitations is documentedhere.
It is recommended that you go through that list.Django spanner has a set of limitations as well, which you can findhere.Features from spanner that are not supported in Django-spanner are listedhere.