Django Engineering

Building Research-Ready Web Applications with Django

Why Django suits data science applications, and the habits that keep them reliable once real people depend on them.

By Nijiati Abulizi2 min read

Many research tools begin as a notebook and stall there. Turning one into something other people can use is mostly unglamorous engineering: accounts, data integrity, deployment. Django supplies most of that, which is why I reach for it.

Let the models be the data dictionary

A Django model is a schema you can read. Field types, constraints and help text document the data in the same place that enforces it.

from django.db import models


class Study(models.Model):
    title = models.CharField(max_length=200)
    cohort_size = models.PositiveIntegerField()
    published = models.DateField(null=True, blank=True)

    class Meta:
        ordering = ["-published"]

    def __str__(self):
        return self.title

Migrations then give you a versioned history of every schema change, which is exactly what you want when a result has to be traced back months later.

Use the admin as your first interface

Registering a model gives you a searchable, permissioned data-entry screen for free. For internal tools that is often the whole user interface for the first few months, and it lets you learn what people need before you design anything custom.

Keep analysis out of the request

A web request should return in well under a second; a model fit will not. Precompute what you can and serve the result. The music explorer on this site works that way: pandas prepares the data ahead of time and the browser draws the charts, so there is no separate service to keep warm. For heavier work, run it as a background job and store the output.

One codebase, several sites

If two products share most of their logic, resist forking. BitSize Learning serves three sites from one Django codebase: a small middleware reads the host name and sets the brand, and templates, navigation and copy follow from that. One login, one deployment and one place to fix a bug.

Make deployment boring

  • Configuration comes from environment variables, never from the repository.
  • PostgreSQL in production, and the same engine in testing where it matters.
  • Static files served with hashed names so they can be cached for a long time.
  • Tests for the paths that would be embarrassing to break: sign-in, payment, data export.

Boring infrastructure is a feature. It is what lets a small team, or one person, keep a tool running while building the next one.

All insights