Skip to main content

A Django app that manages configurations per-site on a multi-site setup. These configs can be made avaialble to users of different sites or administerd via the Django admin.

Project description

Python Tests

Publish Python distribution to PyPI

Django Site Configs

Django Site Configs stores small, structured configuration objects per Django site. It uses django.contrib.sites to choose the active site and stores all config objects for that site in one SiteConfigModel.value JSON field.

The package is intended for project or app settings that should live in the database instead of in settings.py, while still being represented in Python as simple classes with defaults and optional form classes.

Requirements

  • Python 3.10+
  • Django 5.2 or Django 6.0
  • django.contrib.sites

The package dependency range is currently:

django>=5.2,<6.1

Installation

Install the package, then add the sites framework and siteconfigs to INSTALLED_APPS.

INSTALLED_APPS = [
    "django.contrib.sites",
    "siteconfigs",
]

SITE_ID = 1

Run migrations:

python manage.py migrate

The model is registered with the Django admin as Site Setting.

Defining A Config

Create a class that subclasses SiteConfigBaseClass. Public class attributes become default config values.

from django import forms
from django.utils.translation import gettext_lazy as _
from siteconfigs.config import SiteConfigBaseClass


class FeatureFlagsForm(forms.Form):
    show_banner = forms.BooleanField(required=False)


class FeatureFlags(SiteConfigBaseClass):
    _label = _("Feature flags")
    _form_class = FeatureFlagsForm

    show_banner = False

Instantiate the class to load values for the current site:

flags = FeatureFlags()

if flags.show_banner:
    ...

Pass a Site instance to load or save config for a specific site:

from django.contrib.sites.models import Site

site = Site.objects.get(domain="example.com")
flags = FeatureFlags(site=site)

Saving Values

Set attributes on the config object and call save().

flags = FeatureFlags()
flags.show_banner = True
flags.save()

Calling delete() removes only that config class's data from the site's JSON record. It does not delete the SiteConfigModel row.

FeatureFlags().delete()

Optional Forms

If a config class sets _form_class, get_form() returns that form class. This is useful when building custom admin or project views around a config.

config = FeatureFlags()
form_class = config.get_form()
form = form_class(initial={"show_banner": config.show_banner})

Storage Format

For each site, the package creates or updates one SiteConfigModel row with:

  • site: the Django Site
  • key: the config namespace, defaulting to "default"
  • value: a JSON object keyed by each config class's full import path

Example value:

{
  "myapp.config.FeatureFlags": {
    "data": {
      "show_banner": true
    }
  }
}

The namespace stored in SiteConfigModel.key can be changed with:

DJANGO_SITE_CONFIGS_SITE_CONFIG_NAME = "staging"

This lets one site have multiple named config records, such as default and staging.

Current Behavior And Limits

  • The current site is resolved with Site.objects.get_current() when no site is passed to the config class.
  • Config values are plain JSON-serializable public attributes.
  • Saving includes all non-callable public attributes on the config instance.
  • The package provides the storage model and base class, but not a generic UI for editing every config class.
  • The deleted field on SiteConfigModel is available for project-level soft delete workflows, but SiteConfigBaseClass.delete() removes the config data from the JSON value directly.

Development

Install test dependencies and run the test suite:

python -m pip install -e ".[test]"
python -m pytest src/siteconfigs develop

Check for missing migrations:

python develop/manage.py makemigrations --check --dry-run

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_site_configs-0.5.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

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

django_site_configs-0.5.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file django_site_configs-0.5.0.tar.gz.

File metadata

  • Download URL: django_site_configs-0.5.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_site_configs-0.5.0.tar.gz
Algorithm Hash digest
SHA256 d2a9cf5efa1b3e72080d6c4730ee96262068b5db03c72797c9c0a4f87db1d000
MD5 5402eca319e7e7a00660031b2cc9ff4d
BLAKE2b-256 69c5b83b3ad23546a318e7e6767b341bf51b33ce886c186d79f3c8510fe84295

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_site_configs-0.5.0.tar.gz:

Publisher: python-publish.yml on renderbox/django-site-configs

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_site_configs-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_site_configs-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a90c18a811653dcc44e346995d0dac2bc08f063128cabb007b0f2c35a6c794ac
MD5 fa288844f38609698a924be9fd74b518
BLAKE2b-256 045b992caab687a758ff5258b0395398b0d53c889362843fba9b7bcd76bcd179

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_site_configs-0.5.0-py3-none-any.whl:

Publisher: python-publish.yml on renderbox/django-site-configs

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