Skip to content

Instantly share code, notes, and snippets.

@marteinn
Created February 13, 2024 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marteinn/f8285eb8b1a76fa484c4c54bd60fc9ee to your computer and use it in GitHub Desktop.
Save marteinn/f8285eb8b1a76fa484c4c54bd60fc9ee to your computer and use it in GitHub Desktop.
How to add json field to django-constance

How to add a json field to django-constance

  1. Install django-jsonform

    pip install django-jsonform
  2. Add django-jsonform to installed apps

    INSTALLED_APPS = [
        ...
        "django_jsonform",
        ...
    ]
  3. Add additional field to constance in django settings

CONSTANCE_ADDITIONAL_FIELDS = {
    "my_band_list": [
        "django_jsonform.forms.fields.JSONFormField",
        {
            "schema": {
                "type": "array",
                "items": {
                    "type": "dict",
                    "keys": {
                        "genre": {
                            "type": "string",
                        },
                        "band": {
                            "type": "string",
                        },
                    },
                },
            }
        },
    ],
}
  1. Add constance field in django settings

    CONSTANCE_CONFIG = {
        "MY_FAVORITE_BANDS": (
            [{"genre": "Krautrock", "band": "Can"}],
            "List of my favorite bands",
            "my_band_list",
        ),
    }
  2. Done! You should now have a json editable field in constance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment