{"id":4504,"library":"django-constance","title":"Django Constance","description":"Django Constance is a Django app that enables dynamic settings management, allowing configuration values to be edited directly from the Django admin interface without requiring code changes or redeployments. It supports pluggable backends, including built-in Redis and database options, and is actively maintained with regular releases. The current version is 4.3.5.","status":"active","version":"4.3.5","language":"en","source_language":"en","source_url":"https://github.com/jazzband/django-constance","tags":["Django","settings","configuration","Redis","database","admin","live-settings"],"install":[{"cmd":"pip install \"django-constance[redis]\"","lang":"bash","label":"For Redis backend (recommended)"},{"cmd":"pip install \"django-constance[database]\"","lang":"bash","label":"For Database backend"}],"dependencies":[{"reason":"Core framework dependency. Requires Django >=3.2 for Constance v3.0.0+.","package":"Django","optional":false},{"reason":"Required for the Redis backend.","package":"redis","optional":true}],"imports":[{"symbol":"config","correct":"from constance import config"}],"quickstart":{"code":"import os\n\n# settings.py\n# Add 'constance' to your INSTALLED_APPS.\nINSTALLED_APPS = [\n    # ... other Django apps\n    'constance',\n    # 'unfold.contrib.constance', # If using Django Unfold, place before 'constance'\n]\n\n# Configure backend (example for Redis)\nCONSTANCE_BACKEND = 'constance.backends.redisd.RedisBackend'\nCONSTANCE_REDIS_CONNECTION = {\n    'host': os.environ.get('REDIS_HOST', 'localhost'),\n    'port': int(os.environ.get('REDIS_PORT', 6379)),\n    'db': int(os.environ.get('REDIS_DB', 0)),\n}\n\n# Define your dynamic settings\nCONSTANCE_CONFIG = {\n    'SITE_NAME': ('My Awesome Site', 'The name of the website.'),\n    'MAINTENANCE_MODE': (False, 'Enable or disable maintenance mode.'),\n    'MAX_USERS': (1000, 'Maximum allowed users on the platform.'),\n}\n\n# In your Django view or anywhere in Python code:\nfrom constance import config\n\ndef my_view(request):\n    if config.MAINTENANCE_MODE:\n        return HttpResponse(\"Site is under maintenance.\")\n    site_name = config.SITE_NAME\n    max_users = config.MAX_USERS\n    print(f\"Current site name: {site_name}\")\n    print(f\"Max users: {max_users}\")\n    # ... your view logic\n\n# Run Django migrations if using the database backend\n# python manage.py migrate","lang":"python","description":"Install django-constance with your chosen backend (e.g., Redis). Add `constance` to your `INSTALLED_APPS`. Define your dynamic settings using the `CONSTANCE_CONFIG` dictionary in your `settings.py`, specifying a default value and a help text. Then, access these settings anywhere in your Python code by importing `config` from `constance` and using attribute access (e.g., `config.SITE_NAME`). If using the database backend, remember to run `python manage.py migrate`."},"warnings":[{"fix":"Ensure your project runs Python >=3.7 and Django >=3.2. Remove `'constance.backends.database'` from `INSTALLED_APPS` if you are using the database backend.","message":"Version 3.0.0 introduced backward-incompatible changes, dropping support for Python <3.7 and Django <3.2. Additionally, if you were using the database backend, the `constance.backends.database` entry must be *removed* from `INSTALLED_APPS`.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Adjust the order in `INSTALLED_APPS` to place `'constance'` (and `'unfold.contrib.constance'` if applicable) before other admin-related apps or extensions.","message":"When using admin extensions (like Grapelli) or integrations (like `unfold.contrib.constance`), the order of `constance` in `INSTALLED_APPS` matters. It should typically be placed *before* these extensions to ensure correct template loading.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Set `CONSTANCE_IGNORE_ADMIN_VERSION_CHECK = True` in your `settings.py` to skip hash verification. Be aware of the implications if security is a major concern.","message":"In multi-instance deployments (e.g., Docker Swarm, Kubernetes), hashes generated by `django-constance` might differ between instances, preventing data from being saved via the admin.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid accessing `config` synchronously within async views. Consider refactoring to retrieve settings outside the async context or using an async-compatible method if available (though `django-constance` primarily offers synchronous access).","message":"Synchronous access to `config` (e.g., `config.MY_SETTING`) within asynchronous Django views is highly discouraged. It can block the event loop, degrade performance, and for the Database backend, Django's safety guards might raise a `SynchronousOnlyOperation` error.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Pass each component of a `MultiValueField` as a distinct argument to the `constance set` management command.","message":"When setting `MultiValueField` settings via the `constance` management command, separate field values must be provided as individual arguments, not as a single string. E.g., `manage.py constance set DATETIME_VALUE '2011-09-24' '12:30:25'` instead of `'2011-09-24 12:30:25'`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}