Skip to content

Add docs for connecting to MySQL with Django. #806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,50 @@ Base = declarative_base()
To learn more about integrating a database into your FastAPI application,
follow along the [FastAPI SQL Database guide](https://fastapi.tiangolo.com/tutorial/sql-databases/#create-the-database-models).

#### Django

[Django](https://djangoproject.com) is a high-level Python web framework that
encourages rapid development and clean, pragmatic design.

To use the Cloud SQL Connector with Django, you need to create a custom database
backend which subclasses the existing MySQL backend. (Note, at present Postgres
is not supported because Django requires the psycopg2 driver, which is not
currently compatible with the connector.)

Create a `cloudsql` directory in your project with a blank `__init__.py` and a
`base.py` containing the following code:

```python
from django.db.backends.mysql import base
from google.cloud.sql.connector import Connector


class DatabaseWrapper(base.DatabaseWrapper):
def get_new_connection(self, conn_params):
return Connector().connect(**conn_params)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this called on every new database connection? If so I'm not sure this is ideal as the first call to connect() after a new Connector() is initialized it will cache the Cloud SQL instance data such as instance IP and certs used for SSL connection. Initializing a new Connector() on every database connection may cause SQL Admin API quotas to be exhausted and will not take advantage of the caching I mentioned. I will have to think about this a bit

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking at this a bit closer I think we would want the Connector() to be initialized outside the DatabaseWrapper class so that it is only called once and can properly cache instances and re-use them.

Something that looks like this...

from django.db.backends.mysql import base
from google.cloud.sql.connector import Connector

connector = Connector()

class DatabaseWrapper(base.DatabaseWrapper):
    def get_new_connection(self, conn_params):
        return connector.connect(**conn_params)

```

Then in your settings.py file, set your `DATABASES` setting as follows:

```python
DATABASES = {
"default": {
"ENGINE": "cloudsql",
"USER": "...",
"PASSWORD": "...",
"NAME": "...",
"OPTIONS": {
"driver": "pymysql",
"instance_connection_string": "project:region:instance"
}
}

# Needed because Django does not support PyMySQL out of the box
import pymysql
pymysql.install_as_MySQLdb()
```


### Async Driver Usage

The Cloud SQL Connector is compatible with
Expand Down