django-json-schema-editor


Namedjango-json-schema-editor JSON
Version 0.7.1 PyPI version JSON
download
home_pageNone
SummaryDjango widget for using @json-editor/json-editor in the admin
upload_time2025-09-05 13:12:53
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Django JSON Schema Editor

A powerful Django widget for integrating [`@json-editor/json-editor`](https://www.npmjs.com/package/@json-editor/json-editor) with Django forms and admin interfaces. It provides a rich, schema-based editing experience for JSON data in Django applications.

See [the blog post for the announcement and a screenshot](https://406.ch/writing/django-json-schema-editor/).

## Features

- Schema-based validation for JSON data
- Django admin integration
- Rich text editing capabilities with optional prose editor
- Foreign key references with Django admin lookups
- Referential integrity for JSON data containing model references

## Installation

```bash
pip install django-json-schema-editor
```

For django-prose-editor support (rich text editing):

```bash
pip install django-json-schema-editor[prose]
```

## Usage

### Basic Setup

1. Add `django_json_schema_editor` to your `INSTALLED_APPS`:

```python
INSTALLED_APPS = [
    # ...
    'django_json_schema_editor',
    # ...
]
```

2. Use the `JSONField` in your models:

```python
from django.db import models
from django_json_schema_editor.fields import JSONField

class MyModel(models.Model):
    data = JSONField(
        schema={
            "type": "object",
            "properties": {
                "title": {"type": "string"},
                "description": {"type": "string"},
                "count": {"type": "integer"},
            },
        }
    )
```

### Rich Text Editing

For rich text editing, use the `prose` format:

```python
class MyModel(models.Model):
    data = JSONField(
        schema={
            "type": "object",
            "properties": {
                "title": {"type": "string"},
                "content": {"type": "string", "format": "prose"},
            },
        }
    )
```

#### Configuring Prose Editor Extensions

You can customize which formatting options are available in the prose editor by
specifying extensions in the field options:

```python
class MyModel(models.Model):
    data = JSONField(
        schema={
            "type": "object",
            "properties": {
                "title": {"type": "string"},
                "content": {
                    "type": "string",
                    "format": "prose",
                    "options": {
                        "extensions": {
                            "Bold": True,
                            "Italic": True,
                            # Only Bold and Italic will be available
                            # (core extensions are always included)
                        }
                    }
                },
            },
        }
    )
```

The prose editor always includes core extensions (Document, Paragraph,
HardBreak, Text, Menu). By default, it also includes Bold, Italic, Underline,
Subscript, and Superscript extensions. When you specify custom extensions, only
the core extensions plus your specified extensions will be active.

### Foreign Key References

You can reference Django models in your JSON data:

```python
class MyModel(models.Model):
    data = JSONField(
        schema={
            "type": "object",
            "properties": {
                "title": {"type": "string"},
                "image": {
                    "type": "string",
                    "format": "foreign_key",
                    "options": {
                        "url": "/admin/myapp/image/?_popup=1&_to_field=id",
                    },
                },
            },
        }
    )
```

### Data References and Referential Integrity

One of the most powerful features is the ability to maintain referential
integrity between JSON data and model instances:

```python
from django.db import models
from django_json_schema_editor.fields import JSONField

class Image(models.Model):
    title = models.CharField(max_length=100)
    file = models.FileField(upload_to='images/')

class Article(models.Model):
    data = JSONField(
        schema={
            "type": "object",
            "properties": {
                "title": {"type": "string"},
                "content": {"type": "string", "format": "prose"},
                "featured_image": {
                    "type": "string",
                    "format": "foreign_key",
                    "options": {
                        "url": "/admin/myapp/image/?_popup=1&_to_field=id",
                    },
                },
            },
        }
    )

def get_image_ids(article):
    if image_id := article.data.get("featured_image"):
        return [int(image_id)]
    return []

# Register the reference to prevent images from being deleted when they're referenced
Article.register_data_reference(
    Image,
    name="featured_images",
    getter=get_image_ids,
)
```

This prevents a referenced image from being deleted as long as it's referenced in an article's JSON data.

The `name` field will be the name of the underlying `ManyToManyField` which actually references the `Image` instances.

Note that the `get_image_ids` getter has to be written in a very conservative way -- you cannot be sure that the model is valid otherwise. For example, you cannot assume that foreign key values are set (even when they are `null=False`). Django's validation hasn't cleared the model before the getter is invoked for the first time.

## JSON Schema Support

The widget supports the [JSON Schema](https://json-schema.org/) standard for defining the structure and validation rules of your JSON data. Notable supported features include:

- Basic types: string, number, integer, boolean, array, object
- Format validations: date, time, email, etc.
- Custom formats: prose (rich text), foreign_key (model references)
- Required properties
- Enums and default values
- Nested objects and arrays

The [documentation for the json-editor](https://www.npmjs.com/package/@json-editor/json-editor) offers a good overview over all supported features.

## Development

To set up the development environment:

1. Clone the repository
2. Install development dependencies:

```bash
pip install -e ".[tests,prose]"
```

### Code Quality

This project uses several tools to maintain code quality:

- **pre-commit**: We use pre-commit hooks to ensure consistent code style and quality
- **ruff**: For Python linting and formatting
- **biome**: For JavaScript and CSS linting and formatting

To set up pre-commit:

```bash
uv tool install pre-commit
pre-commit install
```

The pre-commit configuration includes:
- Basic file checks (trailing whitespace, merge conflicts, etc.)
- Django upgrade checks
- Ruff for Python linting and formatting
- Biome for JavaScript and CSS linting and formatting
- pyproject.toml validations

### Running Tests

```bash
pytest
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "django-json-schema-editor",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "Matthias Kestenholz <mk@feinheit.ch>, Fabian Germann <fg@feinheit.ch>",
    "download_url": "https://files.pythonhosted.org/packages/8b/5f/4712c8d7771ee67584cd841d319b5efc5bbead1cb520d7c85901b9dfb1c2/django_json_schema_editor-0.7.1.tar.gz",
    "platform": null,
    "description": "# Django JSON Schema Editor\n\nA powerful Django widget for integrating [`@json-editor/json-editor`](https://www.npmjs.com/package/@json-editor/json-editor) with Django forms and admin interfaces. It provides a rich, schema-based editing experience for JSON data in Django applications.\n\nSee [the blog post for the announcement and a screenshot](https://406.ch/writing/django-json-schema-editor/).\n\n## Features\n\n- Schema-based validation for JSON data\n- Django admin integration\n- Rich text editing capabilities with optional prose editor\n- Foreign key references with Django admin lookups\n- Referential integrity for JSON data containing model references\n\n## Installation\n\n```bash\npip install django-json-schema-editor\n```\n\nFor django-prose-editor support (rich text editing):\n\n```bash\npip install django-json-schema-editor[prose]\n```\n\n## Usage\n\n### Basic Setup\n\n1. Add `django_json_schema_editor` to your `INSTALLED_APPS`:\n\n```python\nINSTALLED_APPS = [\n    # ...\n    'django_json_schema_editor',\n    # ...\n]\n```\n\n2. Use the `JSONField` in your models:\n\n```python\nfrom django.db import models\nfrom django_json_schema_editor.fields import JSONField\n\nclass MyModel(models.Model):\n    data = JSONField(\n        schema={\n            \"type\": \"object\",\n            \"properties\": {\n                \"title\": {\"type\": \"string\"},\n                \"description\": {\"type\": \"string\"},\n                \"count\": {\"type\": \"integer\"},\n            },\n        }\n    )\n```\n\n### Rich Text Editing\n\nFor rich text editing, use the `prose` format:\n\n```python\nclass MyModel(models.Model):\n    data = JSONField(\n        schema={\n            \"type\": \"object\",\n            \"properties\": {\n                \"title\": {\"type\": \"string\"},\n                \"content\": {\"type\": \"string\", \"format\": \"prose\"},\n            },\n        }\n    )\n```\n\n#### Configuring Prose Editor Extensions\n\nYou can customize which formatting options are available in the prose editor by\nspecifying extensions in the field options:\n\n```python\nclass MyModel(models.Model):\n    data = JSONField(\n        schema={\n            \"type\": \"object\",\n            \"properties\": {\n                \"title\": {\"type\": \"string\"},\n                \"content\": {\n                    \"type\": \"string\",\n                    \"format\": \"prose\",\n                    \"options\": {\n                        \"extensions\": {\n                            \"Bold\": True,\n                            \"Italic\": True,\n                            # Only Bold and Italic will be available\n                            # (core extensions are always included)\n                        }\n                    }\n                },\n            },\n        }\n    )\n```\n\nThe prose editor always includes core extensions (Document, Paragraph,\nHardBreak, Text, Menu). By default, it also includes Bold, Italic, Underline,\nSubscript, and Superscript extensions. When you specify custom extensions, only\nthe core extensions plus your specified extensions will be active.\n\n### Foreign Key References\n\nYou can reference Django models in your JSON data:\n\n```python\nclass MyModel(models.Model):\n    data = JSONField(\n        schema={\n            \"type\": \"object\",\n            \"properties\": {\n                \"title\": {\"type\": \"string\"},\n                \"image\": {\n                    \"type\": \"string\",\n                    \"format\": \"foreign_key\",\n                    \"options\": {\n                        \"url\": \"/admin/myapp/image/?_popup=1&_to_field=id\",\n                    },\n                },\n            },\n        }\n    )\n```\n\n### Data References and Referential Integrity\n\nOne of the most powerful features is the ability to maintain referential\nintegrity between JSON data and model instances:\n\n```python\nfrom django.db import models\nfrom django_json_schema_editor.fields import JSONField\n\nclass Image(models.Model):\n    title = models.CharField(max_length=100)\n    file = models.FileField(upload_to='images/')\n\nclass Article(models.Model):\n    data = JSONField(\n        schema={\n            \"type\": \"object\",\n            \"properties\": {\n                \"title\": {\"type\": \"string\"},\n                \"content\": {\"type\": \"string\", \"format\": \"prose\"},\n                \"featured_image\": {\n                    \"type\": \"string\",\n                    \"format\": \"foreign_key\",\n                    \"options\": {\n                        \"url\": \"/admin/myapp/image/?_popup=1&_to_field=id\",\n                    },\n                },\n            },\n        }\n    )\n\ndef get_image_ids(article):\n    if image_id := article.data.get(\"featured_image\"):\n        return [int(image_id)]\n    return []\n\n# Register the reference to prevent images from being deleted when they're referenced\nArticle.register_data_reference(\n    Image,\n    name=\"featured_images\",\n    getter=get_image_ids,\n)\n```\n\nThis prevents a referenced image from being deleted as long as it's referenced in an article's JSON data.\n\nThe `name` field will be the name of the underlying `ManyToManyField` which actually references the `Image` instances.\n\nNote that the `get_image_ids` getter has to be written in a very conservative way -- you cannot be sure that the model is valid otherwise. For example, you cannot assume that foreign key values are set (even when they are `null=False`). Django's validation hasn't cleared the model before the getter is invoked for the first time.\n\n## JSON Schema Support\n\nThe widget supports the [JSON Schema](https://json-schema.org/) standard for defining the structure and validation rules of your JSON data. Notable supported features include:\n\n- Basic types: string, number, integer, boolean, array, object\n- Format validations: date, time, email, etc.\n- Custom formats: prose (rich text), foreign_key (model references)\n- Required properties\n- Enums and default values\n- Nested objects and arrays\n\nThe [documentation for the json-editor](https://www.npmjs.com/package/@json-editor/json-editor) offers a good overview over all supported features.\n\n## Development\n\nTo set up the development environment:\n\n1. Clone the repository\n2. Install development dependencies:\n\n```bash\npip install -e \".[tests,prose]\"\n```\n\n### Code Quality\n\nThis project uses several tools to maintain code quality:\n\n- **pre-commit**: We use pre-commit hooks to ensure consistent code style and quality\n- **ruff**: For Python linting and formatting\n- **biome**: For JavaScript and CSS linting and formatting\n\nTo set up pre-commit:\n\n```bash\nuv tool install pre-commit\npre-commit install\n```\n\nThe pre-commit configuration includes:\n- Basic file checks (trailing whitespace, merge conflicts, etc.)\n- Django upgrade checks\n- Ruff for Python linting and formatting\n- Biome for JavaScript and CSS linting and formatting\n- pyproject.toml validations\n\n### Running Tests\n\n```bash\npytest\n```\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Django widget for using @json-editor/json-editor in the admin",
    "version": "0.7.1",
    "project_urls": {
        "Documentation": "https://github.com/matthiask/django-json-schema-editor#readme",
        "Issues": "https://github.com/matthiask/django-json-schema-editor/issues",
        "Source": "https://github.com/matthiask/django-json-schema-editor/"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "b4df4e86fa56a5e5a853feb78dbf46a91a9ca8dea5d637f405ba4484e5155609",
                "md5": "7f1b4b9590701d6b96cab7e830cac9d5",
                "sha256": "68b5bf5fd14fbbf2b5018f497771132187e693cde6873fc221b81ebec31ff1bc"
            },
            "downloads": -1,
            "filename": "django_json_schema_editor-0.7.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7f1b4b9590701d6b96cab7e830cac9d5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 121389,
            "upload_time": "2025-09-05T13:12:54",
            "upload_time_iso_8601": "2025-09-05T13:12:54.615943Z",
            "url": "https://files.pythonhosted.org/packages/b4/df/4e86fa56a5e5a853feb78dbf46a91a9ca8dea5d637f405ba4484e5155609/django_json_schema_editor-0.7.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "8b5f4712c8d7771ee67584cd841d319b5efc5bbead1cb520d7c85901b9dfb1c2",
                "md5": "ce45e8fbc4dd82ea826fc1b3fc6e2fde",
                "sha256": "a1b1ee098b1c869b52d3622daaf0ba61dd9d3ee4514c753b2ac136b884af2948"
            },
            "downloads": -1,
            "filename": "django_json_schema_editor-0.7.1.tar.gz",
            "has_sig": false,
            "md5_digest": "ce45e8fbc4dd82ea826fc1b3fc6e2fde",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 117160,
            "upload_time": "2025-09-05T13:12:53",
            "upload_time_iso_8601": "2025-09-05T13:12:53.265414Z",
            "url": "https://files.pythonhosted.org/packages/8b/5f/4712c8d7771ee67584cd841d319b5efc5bbead1cb520d7c85901b9dfb1c2/django_json_schema_editor-0.7.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-09-05 13:12:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "matthiask",
    "github_project": "django-json-schema-editor#readme",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "django-json-schema-editor"
}
        
Elapsed time: 4.78480s