To run your code domestically throughout growth and testing, you may have to authenticate with Application Default Credentials, or set the GOOGLE_APPLICATION_CREDENTIALS environment variable to authenticate utilizing a service account. This library delegates authentication to the Cloud Spanner Python consumer library. If you happen to’re already utilizing this or one other consumer library efficiently, you should not should do something new to authenticate out of your Django utility. For extra info, see the client libraries documentation on setting up authentication.
Beneath the hood
Beneath the hood, `django-google-spanner` makes use of the Cloud Spanner Python client library, which communicates with Cloud Spanner through its gRPC API. The consumer library additionally manages Cloud Spanner session lifetimes, and gives sane request timeout and retry defaults.
To assist the Django ORM, we added an implementation of the Python Database API Specification (or DB-API) to the consumer library within the `google.cloud.spanner_dbapi` bundle. This bundle handles Cloud Spanner database connections, gives a normal cursor for iterating over streaming outcomes, and seamlessly retries queries and DML statements in aborted transactions. Sooner or later we hope to make use of this bundle to assist different libraries and ORMs which are appropriate with the DB-API, together with SQLAlchemy.
Django ships with a strong schema model management system often called migrations. Every migration describes a change to a Django mannequin that leads to a schema change. Django tracks migrations in an inside `django_migrations` desk, and contains instruments for migrating information between schema variations and producing migrations robotically from an app’s fashions. `django-google-spanner` gives backend support for Cloud Spanner by changing Django migrations into DDL statements – particularly `CREATE TABLE` and `ALTER TABLE` – to be run at migration time.
Following the Django tutorial, let’s have a look at how the consumer library interacts with the Cloud Spanner API. The instance that follows begins from the “Database setup” step of Tutorial 2, and assumes you have already created the `mysite` and `polls` apps from the primary a part of the tutorial.
After configuring database backend as described above, we will run the preliminary migrations for the challenge: