Skip to main content

A Django database backend and admin integration for Amazon DynamoDB.

Project description

Django DynamoDB Backend

A Django database backend and admin integration for Amazon DynamoDB. Run Django 100% on DynamoDB โ€” no PostgreSQL, MySQL, or SQLite required.

CI Python 3.11+ Django 5.2+ License: MIT

Architecture

flowchart LR
    subgraph Django["Django Application"]
        VIEWS[Views] --> MODELS[DynamoDBModel]
        ADMIN[Admin] --> MODELS
        AUTH[Auth] --> USERS[DynamoUser]
        SESS[Sessions] --> SESSBE[SessionStore]
    end
    
    subgraph DDB["DynamoDB"]
        MODELS --> T1[(App Tables)]
        USERS --> T2[(django_users)]
        SESSBE --> T3[(django_sessions)]
    end
    
    style T1 fill:#4053d6,color:#fff
    style T2 fill:#4053d6,color:#fff
    style T3 fill:#4053d6,color:#fff

Features

  • ๐Ÿš€ 100% DynamoDB: Run Django without any relational database โ€” sessions, users, and admin all on DynamoDB
  • ๐Ÿ” DynamoDB Authentication: Full user auth system with username/email GSIs, password hashing, and permissions
  • ๐Ÿช DynamoDB Sessions: Session backend with automatic TTL-based expiration
  • โšก Django Admin: Complete admin interface support with DynamoDB optimizations
  • ๐Ÿ“ฆ Django ORM Compatible: Familiar QuerySet API, Q objects, aggregations, and more
  • ๐Ÿ”„ Migration System: DynamoDB-specific table and index management
  • ๐Ÿ’ฐ Cost Optimized: Pay-per-request billing, fits within AWS free tier for development
  • โ˜๏ธ Serverless Ready: Perfect for AWS Lambda deployments

Requirements

  • Python 3.11+
  • Django 5.2+
  • boto3, pynamodb
  • Docker (optional โ€” for the demo; not needed for development)

Quick Start

๐ŸŽฏ Try the Demo (30 seconds)

The fastest way to explore โ€” runs entirely on DynamoDB with no other databases:

git clone https://github.com/jpwhite3/django-dynamodb-backend.git
cd django-dynamodb-backend
make demo

This starts:

  • LocalStack (local DynamoDB emulator)
  • Django Admin at http://localhost:8001/admin/
  • Sample data (blog posts, products, orders)

Login: admin / admin123

๐ŸŽ‰ No Redis, PostgreSQL, or SQLite โ€” everything runs on DynamoDB!

Demo commands:

make demo        # Start demo
make demo-stop   # Stop demo
make demo-reset  # Reset and restart
make demo-logs   # View logs

๐Ÿ–ฅ๏ธ Quick Start Without Docker

If you prefer to run locally without Docker, install LocalStack CLI (or use a real AWS account) and run:

git clone https://github.com/jpwhite3/django-dynamodb-backend.git
cd django-dynamodb-backend
pip install -e ".[dev]"

# Start LocalStack in the background (or point to real AWS)
localstack start -d

# Run the test suite
python -m pytest tests/

Installation

# Clone the repository (not yet available on PyPI)
git clone https://github.com/jpwhite3/django-dynamodb-backend.git
cd django-dynamodb-backend

# Install with pip
pip install -e .

Configuration

Option 1: DynamoDB-Only Mode (Recommended)

Run Django entirely on DynamoDB โ€” ideal for serverless deployments:

# settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',           # Required for admin
    'django.contrib.contenttypes',
    'django.contrib.sessions',       # Required for session middleware
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # DynamoDB backend
    'django_dynamodb_backend',
    'django_dynamodb_backend.contrib.auth_dynamo',  # DynamoDB users
]

# Database
DATABASES = {
    'default': {
        'ENGINE': 'django_dynamodb_backend.db',
        'NAME': 'my_app',
        'OPTIONS': {
            'region_name': 'us-east-1',
            'endpoint_url': 'http://localhost:4566',  # LocalStack for dev
            'aws_access_key_id': 'test',
            'aws_secret_access_key': 'test',
        },
    }
}

# Sessions โ€” stored in DynamoDB with TTL
SESSION_ENGINE = 'django_dynamodb_backend.sessions'
DYNAMODB_SESSION_TABLE_NAME = 'django_sessions'

# Authentication โ€” DynamoDB users with GSIs
AUTH_USER_MODEL = 'auth_dynamo.DynamoUser'
DYNAMODB_USER_TABLE_NAME = 'django_users'
AUTHENTICATION_BACKENDS = [
    'django_dynamodb_backend.contrib.auth_dynamo.backends.DynamoAuthBackend',
]

# Cache (local memory โ€” or use ElastiCache in production)
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
    }
}

Create Tables

# Create sessions table (with TTL)
python manage.py dynamodb_create_session_table

# Create users table (with GSIs) and admin user
python manage.py dynamodb_create_user_table --create-admin

# Or create a superuser interactively (like Django's createsuperuser)
python manage.py dynamodb_createsuperuser

# Create your app's tables
python manage.py dynamodb_migrate

Option 2: Hybrid Mode

Use DynamoDB for your models while keeping PostgreSQL/SQLite for Django's built-in apps:

# settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_dynamodb_backend',
]

DATABASES = {
    'default': {
        'ENGINE': 'django_dynamodb_backend.db',
        'NAME': 'my_app',
        'OPTIONS': {
            'region_name': 'us-east-1',
        },
    }
}

Define Models

from django.db import models
from django_dynamodb_backend.models import DynamoDBModel

class BlogPost(DynamoDBModel):
    id = models.CharField(primary_key=True, max_length=36)
    title = models.CharField(max_length=200)
    content = models.TextField()
    author = models.CharField(max_length=100)
    published = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    
    class Meta:
        db_table = 'blog_posts'
    
    def __str__(self):
        return self.title

Admin Interface

from django.contrib import admin
from django_dynamodb_backend.admin import DynamoDBAdmin
from .models import BlogPost

@admin.register(BlogPost)
class BlogPostAdmin(DynamoDBAdmin):
    list_display = ['title', 'author', 'published', 'created_at']
    list_filter = ['published', 'author']
    search_fields = ['title', 'content']

Management Commands

Command Description
dynamodb_create_session_table Create sessions table with TTL
dynamodb_create_user_table Create users table with GSIs
dynamodb_create_user_table --create-admin Also create admin user
dynamodb_migrate Apply DynamoDB migrations
dynamodb_makemigrations Create new migrations
dynamodb_showmigrations Show migration status
dynamodb_rollback Rollback migrations
dynamodb_createsuperuser Create a superuser interactively
dynamodb_performance Monitor DynamoDB performance metrics

DynamoDB Table Schemas

Sessions Table (django_sessions)

  • Partition Key: session_key (String)
  • TTL Attribute: expire_date (Unix timestamp)
  • Billing: Pay-per-request

Users Table (django_users)

  • Partition Key: id (String, UUID)
  • GSI username-index: Lookup by username
  • GSI email-index: Lookup by email
  • Billing: Pay-per-request

Project Structure

django-dynamodb-backend/
โ”œโ”€โ”€ src/django_dynamodb_backend/
โ”‚   โ”œโ”€โ”€ admin.py                 # Django Admin integration
โ”‚   โ”œโ”€โ”€ models.py                # DynamoDB model base class
โ”‚   โ”œโ”€โ”€ managers.py              # QuerySet implementation
โ”‚   โ”œโ”€โ”€ sessions.py              # DynamoDB session backend
โ”‚   โ”œโ”€โ”€ contrib/
โ”‚   โ”‚   โ””โ”€โ”€ auth_dynamo/         # DynamoDB authentication
โ”‚   โ”‚       โ”œโ”€โ”€ models.py        # DynamoUser model
โ”‚   โ”‚       โ”œโ”€โ”€ managers.py      # User manager
โ”‚   โ”‚       โ”œโ”€โ”€ backends.py      # Auth backend
โ”‚   โ”‚       โ”œโ”€โ”€ admin.py         # User admin
โ”‚   โ”‚       โ””โ”€โ”€ forms.py         # User forms
โ”‚   โ”œโ”€โ”€ db/                      # Database backend
โ”‚   โ””โ”€โ”€ management/commands/     # Management commands
โ”œโ”€โ”€ examples/demo_project/       # Demo application
โ”œโ”€โ”€ tests/                       # Test suite
โ””โ”€โ”€ docs/                        # Documentation

Documentation

๐Ÿ“š Documentation Index โ€” Find the right doc for your needs

Document Description
Migration Tutorial Step-by-step guide to migrate existing Django projects
Django Compatibility Supported ORM features and limitations
API Reference Complete API documentation
Deployment Guide Production & serverless deployment
Feature Walkthrough Detailed feature guide with demos

Development

# Setup
git clone https://github.com/jpwhite3/django-dynamodb-backend.git
cd django-dynamodb-backend
pip install -e ".[dev]"

# Lint
black .
isort .
flake8 .

# Test
python -m pytest tests/

Contributing

Contributions welcome! See CONTRIBUTING.md.

License

MIT License โ€” see LICENSE.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_dynamodb_backend-1.0.0rc1.tar.gz (108.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_dynamodb_backend-1.0.0rc1-py3-none-any.whl (108.3 kB view details)

Uploaded Python 3

File details

Details for the file django_dynamodb_backend-1.0.0rc1.tar.gz.

File metadata

File hashes

Hashes for django_dynamodb_backend-1.0.0rc1.tar.gz
Algorithm Hash digest
SHA256 343eb0c1c70954bf633762bac9fec63418e12fcfdfb6092c3332775fdd230524
MD5 897cc7310266e2b66eb26fc99f388836
BLAKE2b-256 f8756fd58e681ad48a903d1e6253fb7552de2a4248997f92d9cf6d44b294b2ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_dynamodb_backend-1.0.0rc1.tar.gz:

Publisher: release.yml on jpwhite3/django-dynamodb-backend

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_dynamodb_backend-1.0.0rc1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_dynamodb_backend-1.0.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 770663695b7d3a717fd7c4d915f2f52f4ec2c92f9799390e9479d4939d6b420c
MD5 8d6f5f595ca6355be345f90fec8d9ad3
BLAKE2b-256 4e7720120d4567aa5ac13337bf20c883f9dc28c13aa0834caee81903ae1ab435

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_dynamodb_backend-1.0.0rc1-py3-none-any.whl:

Publisher: release.yml on jpwhite3/django-dynamodb-backend

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page