Build a Python App with CockroachDB and Django

On this page Carat arrow pointing down
CockroachDB v21.2 is no longer supported. For more details, see the Release Support Policy.

This tutorial shows you how build a simple Python application with CockroachDB and the Django framework.

CockroachDB supports Django versions 3.1+.


The example code and instructions on this page use Python 3.9 and Django 3.1.

Step 1. Start CockroachDB

Create a free cluster

  1. If you haven't already, sign up for a CockroachDB Cloud account.
  2. Log in to your CockroachDB Cloud account.
  3. On the Clusters page, click Create Cluster.
  4. On the Create your cluster page, select Serverless.
  5. Click Create cluster.

    Your cluster will be created in a few seconds and the Create SQL user dialog will display.

Create a SQL user

The Create SQL user dialog allows you to create a new SQL user and password.

  1. Enter a username in the SQL user field or use the one provided by default.
  2. Click Generate & save password.
  3. Copy the generated password and save it in a secure location.
  4. Click Next.

    Currently, all new users are created with full privileges. For more information and to change the default settings, see [Manage SQL users on a cluster.

Get the root certificate

The Connect to cluster dialog shows information about how to connect to your cluster.

  1. Select General connection string from the Select option dropdown.
  2. Open a new terminal on your local machine, and run the CA Cert download command provided in the Download CA Cert section. The client driver used in this tutorial requires this certificate to connect to CockroachDB Cloud.

Get the connection information

  1. Select Parameters only from the Select option dropdown.
  2. Copy the connection information for each parameter displayed and save it in a secure location.
  1. If you haven't already, download the CockroachDB binary.
  2. Run the cockroach start-single-node command:

    $ cockroach start-single-node --advertise-addr 'localhost' --insecure

    This starts an insecure, single-node cluster.

  3. Take note of the following connection information in the SQL shell welcome text:

    CockroachDB node starting at 2021-08-30 17:25:30.06524 +0000 UTC (took 4.3s)
    build:               CCL v21.1.6 @ 2021/07/20 15:33:43 (go1.15.11)
    webui:               http://localhost:8080
    sql:                 postgresql://root@localhost:26257?sslmode=disable

    You'll use the sql connection string to connect to the cluster later in this tutorial.


The --insecure flag used in this tutorial is intended for non-production testing only. To run CockroachDB in production, use a secure cluster instead.

Step 2. Get the sample code

Clone the code's GitHub repo:

$ git clone

The project directory structure should look like this:

├── Dockerfile
├── cockroach_example
│   ├── cockroach_example
│   │   ├──
│   │   ├──
│   │   ├── migrations
│   │   │   ├──
│   │   │   └──
│   │   ├──
│   │   ├──
│   │   ├──
│   │   ├──
│   │   └──
│   └──
└── requirements.txt

Step 3. Install the application requirements

To use CockroachDB with Django, the following modules are required:


The major version of django-cockroachdb must correspond to the major version of django. The minor release numbers do not need to match.

The requirements.txt file at the top level of the example-app-python-django project directory contains a list of the requirements needed to run this application:


This tutorial uses virtualenv for dependency management.

  1. Install virtualenv:

    $ pip install virtualenv
  2. At the top level of the app's project directory, create and then activate a virtual environment:

    $ virtualenv env
    $ source env/bin/activate
  3. Install the modules listed in requirements.txt to the virtual environment:

    $ pip install -r requirements.txt

Step 4. Build out the application

Configure the database connection

Open cockroach_example/cockroach_example/, and configure the DATABASES dictionary to connect to your cluster using the connection information that you retrieved from the CockroachDB Cloud Console.

    'default': {
        'ENGINE': 'django_cockroachdb',
        'NAME': '{database}',
        'USER': '{username}',
        'PASSWORD': '{password}',
        'HOST': '{host}',
        'PORT': '{port}',
        'OPTIONS': {
            'sslmode': 'verify-full',
            'options': '--cluster={routing-id}'

For more information about configuration a Django connection to CockroachDB Serverless, see Connect to a CockroachDB Cluster.

After you have configured the app's database connection, you can start building out the application.


Start by building some models, defined in a file called You can copy the sample code below and paste it into a new file, or you can download the file directly.

from django.db import models
import uuid

class Customers(models.Model):
    id = models.UUIDField(
    name = models.CharField(max_length=250)

class Products(models.Model):
    id = models.UUIDField(
    name = models.CharField(max_length=250)
    price = models.DecimalField(max_digits=18, decimal_places=2)

class Orders(models.Model):
    id = models.UUIDField(
    subtotal = models.DecimalField(max_digits=18, decimal_places=2)
    customer = models.ForeignKey(
        Customers, on_delete=models.CASCADE, null=True)
    product = models.ManyToManyField(Products)

In this file, we define some simple classes that map to the tables in the cluster.


Next, build out some class-based views for the application in a file called You can copy the sample code below and paste it into a new file, or you can download the file directly.

from django.http import JsonResponse, HttpResponse
from django.utils.decorators import method_decorator
from django.views.generic import View
from django.views.decorators.csrf import csrf_exempt
from django.db import Error, IntegrityError
from django.db.transaction import atomic
from psycopg2 import errorcodes
import json
import sys
import time

from .models import *

# Warning: Do not use retry_on_exception in an inner nested transaction.

def retry_on_exception(num_retries=3, on_failure=HttpResponse(status=500), delay_=0.5, backoff_=1.5):
    def retry(view):
        def wrapper(*args, **kwargs):
            delay = delay_
            for i in range(num_retries):
                    return view(*args, **kwargs)
                except IntegrityError as ex:
                    if i == num_retries - 1:
                        return on_failure
                    elif getattr(ex.__cause__, 'pgcode', '') == errorcodes.SERIALIZATION_FAILURE:
                        delay *= backoff_
                except Error as ex:
                    return on_failure
        return wrapper
    return retry

class PingView(View):
    def get(self, request, *args, **kwargs):
        return HttpResponse("python/django", status=200)

@method_decorator(csrf_exempt, name='dispatch')
class CustomersView(View):
    def get(self, request, id=None, *args, **kwargs):
        if id is None:
            customers = list(Customers.objects.values())
            customers = list(Customers.objects.filter(id=id).values())
        return JsonResponse(customers, safe=False)

    def post(self, request, *args, **kwargs):
        form_data = json.loads(request.body.decode())
        name = form_data['name']
        c = Customers(name=name)
        return HttpResponse(status=200)

    def delete(self, request, id=None, *args, **kwargs):
        if id is None:
            return HttpResponse(status=404)
        return HttpResponse(status=200)

    # The PUT method is shadowed by the POST method, so there doesn't seem
    # to be a reason to include it.

@method_decorator(csrf_exempt, name='dispatch')
class ProductView(View):
    def get(self, request, id=None, *args, **kwargs):
        if id is None:
            products = list(Products.objects.values())
            products = list(Products.objects.filter(id=id).values())
        return JsonResponse(products, safe=False)

    def post(self, request, *args, **kwargs):
        form_data = json.loads(request.body.decode())
        name, price = form_data['name'], form_data['price']
        p = Products(name=name, price=price)
        return HttpResponse(status=200)

    # The REST API outlined in the github does not say that /product/ needs
    # a PUT and DELETE method

@method_decorator(csrf_exempt, name='dispatch')
class OrdersView(View):
    def get(self, request, id=None, *args, **kwargs):
        if id is None:
            orders = list(Orders.objects.values())
            orders = list(Orders.objects.filter(id=id).values())
        return JsonResponse(orders, safe=False)

    def post(self, request, *args, **kwargs):
        form_data = json.loads(request.body.decode())
        c = Customers.objects.get(id=form_data['customer']['id'])
        o = Orders(subtotal=form_data['subtotal'], customer=c)
        for p in form_data['products']:
            p = Products.objects.get(id=p['id'])
        return HttpResponse(status=200)

This file defines the application's views as classes. Each view class corresponds to one of the table classes defined in The methods of these classes define read and write transactions on the tables in the database.

Importantly, the file defines a transaction retry loop in the decorator function retry_on_exception(). This function decorates each view method, ensuring that transaction ordering guarantees meet the ANSI SERIALIZABLE isolation level. For more information about how transactions (and retries) work, see Transactions.

URL routes

Lastly, define some URL routes in a file called You can copy the sample code below and paste it into the existing file, or you can download the file directly and replace the existing one.

from django.contrib import admin
from django.urls import path

from .views import CustomersView, OrdersView, PingView, ProductView

urlpatterns = [

    path('ping/', PingView.as_view()),

    # Endpoints for customers URL.
    path('customer/', CustomersView.as_view(), name='customers'),
    path('customer/<uuid:id>/', CustomersView.as_view(), name='customers'),

    # Endpoints for customers URL.
    path('product/', ProductView.as_view(), name='product'),
    path('product/<uuid:id>/', ProductView.as_view(), name='product'),

    path('order/', OrdersView.as_view(), name='order'),

Step 5. Initialize the database

In the top cockroach_example directory, use the script to create Django migrations that initialize the database for the application:

$ python makemigrations cockroach_example
$ python migrate

This initializes the tables defined in, in addition to some other tables for the admin functionality included with Django's starter application.

Step 6. Run the app

  1. In a different terminal, navigate to the top of the cockroach_example directory, and start the app:

    $ python runserver

    The output should look like this:

    Starting development server at
    Quit the server with CONTROL-C.

    To perform simple reads and writes to the database, you can send HTTP requests to the application server listening at

  2. In a new terminal, use curl to send a POST request to the application:

    $ curl --header "Content-Type: application/json" \
    --request POST \
    --data '{"name":"Carl"}'

    This request inserts a new row into the cockroach_example_customers table.

  3. Send a GET request to read from the cockroach_example_customers table:

    $ curl
    [{"id": "bb7d6c4d-efb3-45f8-b790-9911aae7d8b2", "name": "Carl"}]

    You can also query the table directly in the SQL shell to see the changes:

    > SELECT * FROM cockroach_example_customers;
                       id                  | name
      bb7d6c4d-efb3-45f8-b790-9911aae7d8b2 | Carl
    (1 row)
  4. Enter Ctrl+C to stop the application.

What's next?

Read more about writing a Django app.

You might also be interested in the following pages:

Yes No
On this page

Yes No