Django widget for using @json-editor/json-editor in the admin
Project description
Django JSON Schema Editor
A powerful Django widget for integrating @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.
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
pip install django-json-schema-editor
For django-prose-editor support (rich text editing):
pip install django-json-schema-editor[prose]
Usage
Basic Setup
- Add
django_json_schema_editor
to yourINSTALLED_APPS
:
INSTALLED_APPS = [
# ...
'django_json_schema_editor',
# ...
]
- Use the
JSONField
in your models:
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:
class MyModel(models.Model):
data = JSONField(
schema={
"type": "object",
"properties": {
"title": {"type": "string"},
"content": {"type": "string", "format": "prose"},
},
}
)
Foreign Key References
You can reference Django models in your JSON data:
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:
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.
JSON Schema Support
The widget supports the JSON Schema 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 offers a good overview over all supported features.
Development
To set up the development environment:
- Clone the repository
- Install development dependencies:
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:
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
pytest
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_json_schema_editor-0.4.0.tar.gz
.
File metadata
- Download URL: django_json_schema_editor-0.4.0.tar.gz
- Upload date:
- Size: 116.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc547327d7a1d16100d5948650ee0a6f8d6f39e5b62007b6c9cd6dd8b13fecd8 |
|
MD5 | e07a6a1ebf4edd74b8943885afc976e9 |
|
BLAKE2b-256 | cba4f5318581c3a1a9e28db7dd58677861af06bc93e642eab7a2b95ef38e8f1c |
File details
Details for the file django_json_schema_editor-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: django_json_schema_editor-0.4.0-py3-none-any.whl
- Upload date:
- Size: 120.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a61677397eb94e5323e2efd8a96d419e1b6405ebe14aad18dc05dac7585795a5 |
|
MD5 | 854ab5607a704be7e9d7152f80fa904e |
|
BLAKE2b-256 | 068a31f03c5beba14c0da6cbd9e2e59ef09e525ca088165af2e1c253354c7722 |