Skip to main content

A Django + DRF integration for LaunchDarkly feature flags

Project description

Django LaunchDarkly Flags

A Django + DRF integration for LaunchDarkly feature flags with built-in REST API support.

Installation

pip install launchdarkly-drf

Quick Start

1. Add to INSTALLED_APPS

# settings.py
INSTALLED_APPS = [
    # ...
    'launchdarkly_drf',
]

# Configure your LaunchDarkly SDK key
LAUNCHDARKLY_SDK_KEY = 'your-sdk-key-here'

# Optional: disable LaunchDarkly in tests
LAUNCHDARKLY_OFFLINE = False  # Set to True in test settings

2. Use Feature Flags in Your Code

from launchdarkly_drf import has_feature, get_flag_value

def my_view(request):
    # Boolean flags
    if has_feature('new-ui-enabled', request=request):
        return render_new_ui(request)

    # String/number/JSON flags
    theme = get_flag_value('ui-theme', request=request)
    max_items = get_flag_value('max-items-per-page', request=request)
    config = get_flag_value('feature-config', request=request)

3. (Optional) Add REST API Endpoint

Expose flags to your frontend:

# urls.py
from rest_framework.routers import DefaultRouter
from launchdarkly_drf import FeatureFlagViewSet

router = DefaultRouter()
router.register(r'feature-flags', FeatureFlagViewSet, basename='feature-flags')

urlpatterns = [
    path('api/', include(router.urls)),
]

Now you can GET /api/feature-flags/ to retrieve all flags as JSON.

Usage Guide

Boolean Feature Flags

Use has_feature() for simple on/off toggles:

from launchdarkly_drf import has_feature

# With request (automatically extracts user context)
if has_feature('beta-features', request=request):
    enable_beta_features()

# With custom context
if has_feature('beta-features', context={'key': 'user@example.com', 'custom': 'value'}):
    enable_beta_features()

# Anonymous context (no targeting)
if has_feature('public-feature'):
    show_public_feature()

Multi-Variant Flags

Use get_flag_value() for string, number, or JSON flags:

from launchdarkly_drf import get_flag_value

# String flags (e.g., A/B test variants)
variant = get_flag_value('ui-variant', request=request)
if variant == 'blue':
    render_blue_theme()
elif variant == 'green':
    render_green_theme()

# Number flags
max_retries = get_flag_value('max-retries', request=request)

# JSON flags
config = get_flag_value('feature-config', request=request)
timeout = config.get('timeout', 30)

Get All Flags

Retrieve all flags at once (useful for frontend):

from launchdarkly_drf import get_all_flag_values

flags = get_all_flag_values(request)
# Returns: {'flag-1': True, 'flag-2': 'variant-a', 'flag-3': 42, ...}

Testing

Use the patch_feature_flag decorator to mock flags in tests:

from launchdarkly_drf.testing import patch_feature_flag
from launchdarkly_drf import has_feature, get_flag_value

@patch_feature_flag('new-ui-enabled', True)
@patch_feature_flag('ui-theme', 'dark')
def test_my_feature():
    assert has_feature('new-ui-enabled') is True
    assert get_flag_value('ui-theme') == 'dark'

You can stack multiple decorators to patch multiple flags.

Advanced Configuration

Custom User Context

By default, the package extracts the user's email from request.user.email. You can provide custom context:

from launchdarkly_drf import has_feature

# Custom targeting
context = {
    'key': 'user-123',
    'email': 'user@example.com',
    'custom': {
        'plan': 'enterprise',
        'region': 'us-west',
    }
}

if has_feature('enterprise-feature', context=context):
    show_enterprise_features()

How It Works

  1. Initialization: The LaunchDarkly client is initialized when Django starts (in AppConfig.ready())
  2. Singleton Pattern: A singleton LaunchDarkly client instance is shared across your app
  3. User Context: User information is automatically extracted from drf requests for flag targeting
  4. Offline Mode: Set LAUNCHDARKLY_OFFLINE=True in test settings to disable LaunchDarkly connections

Support

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

launchdarkly_drf-0.1.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

launchdarkly_drf-0.1.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file launchdarkly_drf-0.1.0.tar.gz.

File metadata

  • Download URL: launchdarkly_drf-0.1.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for launchdarkly_drf-0.1.0.tar.gz
Algorithm Hash digest
SHA256 58d66d522ecbf15448c4c3fe5fdf2183a4b850e255bee12629a64295afcf4d6d
MD5 91a155605be50da92a0c70ba1446c8db
BLAKE2b-256 720f0d89c375a248902326e00c0a603dad80e82079af15f625fe6afb5fb5b512

See more details on using hashes here.

File details

Details for the file launchdarkly_drf-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: launchdarkly_drf-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for launchdarkly_drf-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 754aad6976ccd3ba7ba7dc74db73dace56eb41bd9fc3af64e8dc7c7b17c0898c
MD5 ce4cf027ebf981b1ba1d0b2861e36d23
BLAKE2b-256 ddb3a11864ce9fcf53e2de78fe2bda7b7650365fb62b881dce74cff8c3d06255

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