Skip to main content

A lightweight django package designed to connect Django applications to Supabase Auth, Database Access, and Storage

Project description

Django Supabase

Professional, production-ready Supabase integration for Django.

Features

  • 🔐 Authentication: Seamless Supabase Auth + Django User model sync
  • 💾 Database: Direct PostgreSQL connection with Django ORM
  • 📦 Storage: Django storage backends for Supabase Storage
  • 🚀 Production-Ready: Connection pooling, caching, error handling
  • 🔧 Django-Native: Uses Django's ecosystem and patterns

Installation

pip install django-supabase

Quick Setup

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'django_supabase',
    'django_supabase.auth',
]

Configure Settings

SUPABASE_URL = 'https://your-project.supabase.co'
SUPABASE_KEY = 'your-publishable-or-anon-key'
SUPABASE_SECRET_KEY = 'your-secret-or-service-role-key'  # server-side only

# Recommended default: ask Supabase Auth to validate bearer tokens.
SUPABASE_JWT_VERIFICATION = 'auth_server'

# Optional for projects using asymmetric JWT signing keys:
# SUPABASE_JWT_VERIFICATION = 'jwks'
# SUPABASE_JWT_ALGORITHMS = ['RS256', 'ES256']

# Legacy/local-only fallback for HS256 shared-secret verification:
# SUPABASE_JWT_VERIFICATION = 'jwt_secret'
# SUPABASE_JWT_SECRET = 'your-jwt-secret'

AUTH_USER_MODEL = 'supabase_auth.SupabaseUser'

AUTHENTICATION_BACKENDS = [
    'django_supabase.auth.backends.SupabaseAuthBackend',
    'django.contrib.auth.backends.ModelBackend',
]

DATABASES = {
    'default': {
        'ENGINE': 'django_supabase.db.engine.SupabasePostgreSQLEngine',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'your-password',
        'HOST': 'db.your-project.supabase.co',
        'PORT': 5432,
    }
}

STORAGES = {
    'default': {
        'BACKEND': 'django_supabase.storage.backends.SupabaseMediaStorage',
    },
    'staticfiles': {
        'BACKEND': 'django_supabase.storage.backends.SupabaseStaticStorage',
    },
}

Run Migrations

python manage.py migrate
python manage.py create_storage_buckets

Usage Example

Authentication

from django.contrib.auth import authenticate

# Email/Password login
user = authenticate(email='user@example.com', password='password')

# JWT Token authentication (in views)
# Token automatically extracted from Authorization: Bearer <token>

Frontend OAuth

Use Supabase Auth in the frontend with a publishable key. After OAuth login, send the Supabase session access token to Django:

const { data, error } = await supabase.auth.signInWithOAuth({
  provider: 'google',
  options: { redirectTo: 'https://your-frontend.example.com/auth/callback' },
})

const { data: sessionData } = await supabase.auth.getSession()

await fetch('/api/me/', {
  headers: {
    Authorization: `Bearer ${sessionData.session.access_token}`,
  },
})

Django validates the bearer token through SupabaseAuthMiddleware and syncs the local user by Supabase sub / user.id, not by email alone.

File Upload

from django_supabase.storage.utils import upload_file_to_supabase

file = ...

url = upload_file_to_supabase(file, 'media', path='uploads/')

Direct Supabase Queries

from django_supabase.db.client import get_supabase_client

supabase = get_supabase_client()
response = supabase.table('posts').select('*').execute()

Test

# supabase_integration/tests.py
from django.test import TestCase
from django.contrib.auth import get_user_model
from django_supabase.db.client import get_supabase_client

User = get_user_model()

class SupabaseIntegrationTestCase(TestCase):
    """Base test case with Supabase utilities"""
    
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.supabase = get_supabase_client()
    
    def create_test_user(self, email='test@example.com'):
        """Helper to create test users"""
        return User.objects.create_user(
            username=email,
            email=email,
            password='testpass123'
        )

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_supabase-1.0.0.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

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

django_supabase-1.0.0-py3-none-any.whl (22.9 kB view details)

Uploaded Python 3

File details

Details for the file django_supabase-1.0.0.tar.gz.

File metadata

  • Download URL: django_supabase-1.0.0.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for django_supabase-1.0.0.tar.gz
Algorithm Hash digest
SHA256 07a96a24837c405c550d7b99fea529ffff20fc840fa69a94575378ed87147144
MD5 8d19c03f0debff9bf65c39f3e06c5750
BLAKE2b-256 a62b4f21d5af64bb9ede791d8a64e623203dc05231577d6066d94329c0367830

See more details on using hashes here.

File details

Details for the file django_supabase-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_supabase-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b2f482d422a1dd7eb7ebd5415bf96adbd4a4c6d92781739528abaebfe49f4047
MD5 f2e5141c97bd639dceefd7c6f68bafc3
BLAKE2b-256 d6e736c74676f406ab653e0fba6ffbbd82da696d7b3d3e6cba2d2e13d894d462

See more details on using hashes here.

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