A simple Django app to add JSON widget into Django Administration.
Project description
# Django Administration JSON Editor
[![Build Status](https://travis-ci.org/abogushov/django-admin-json-editor.svg?branch=master)](https://travis-ci.org/abogushov/django-admin-json-editor)
Application adds support for editing JSONField in Django Administration via https://github.com/jdorn/json-editor.
## Quick start
Install application via pip:
```bash
pip install django-admin-json-editor
```
Add application to the INSTALLED_APPS settings:
```python
INSTALLED_APPS = [
...
'django_admin_json_editor',
...
]
```
Define schema of json field:
```python
DATA_SCHEMA = {
'type': 'object',
'title': 'Data',
'properties': {
'text': {
'title': 'Some text',
'type': 'string',
'format': 'textarea',
},
'status': {
'title': 'Status',
'type': 'boolean',
},
},
}
```
Use JSONEditorWidget to bind editor to the form field:
```python
class JSONModelAdminForm(forms.ModelForm):
class Meta:
model = JSONModel
fields = '__all__'
widgets = {
'data': JSONEditorWidget(DATA_SCHEMA, collapsed=False),
}
```
### Dynamic schema
It is possible to build dynamic schema for widget:
```python
def dynamic_schema(widget):
return {
'type': 'array',
'title': 'tags',
'items': {
'type': 'string',
'enum': [i for i in Tag.objects.values_list('name', flat=True)],
}
}
```
```python
@admin.register(JSONModel)
class JSONModelAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
widget = JSONEditorWidget(dynamic_schema, False)
form = super().get_form(request, obj, widgets={'tags': widget}, **kwargs)
return form
```
[![Build Status](https://travis-ci.org/abogushov/django-admin-json-editor.svg?branch=master)](https://travis-ci.org/abogushov/django-admin-json-editor)
Application adds support for editing JSONField in Django Administration via https://github.com/jdorn/json-editor.
## Quick start
Install application via pip:
```bash
pip install django-admin-json-editor
```
Add application to the INSTALLED_APPS settings:
```python
INSTALLED_APPS = [
...
'django_admin_json_editor',
...
]
```
Define schema of json field:
```python
DATA_SCHEMA = {
'type': 'object',
'title': 'Data',
'properties': {
'text': {
'title': 'Some text',
'type': 'string',
'format': 'textarea',
},
'status': {
'title': 'Status',
'type': 'boolean',
},
},
}
```
Use JSONEditorWidget to bind editor to the form field:
```python
class JSONModelAdminForm(forms.ModelForm):
class Meta:
model = JSONModel
fields = '__all__'
widgets = {
'data': JSONEditorWidget(DATA_SCHEMA, collapsed=False),
}
```
### Dynamic schema
It is possible to build dynamic schema for widget:
```python
def dynamic_schema(widget):
return {
'type': 'array',
'title': 'tags',
'items': {
'type': 'string',
'enum': [i for i in Tag.objects.values_list('name', flat=True)],
}
}
```
```python
@admin.register(JSONModel)
class JSONModelAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
widget = JSONEditorWidget(dynamic_schema, False)
form = super().get_form(request, obj, widgets={'tags': widget}, **kwargs)
return form
```
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
django-admin-json-editor-0.1.5.tar.gz
(935.4 kB
view hashes)
Close
Hashes for django-admin-json-editor-0.1.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | cdec9392d1082df3a18db299c7dd02a1b68abefa4afdb5c0f5872c6734073fb6 |
|
MD5 | f700c5739d093b65182500789e860fcf |
|
BLAKE2b-256 | 61e3bd8d48485f07fb827e84ac58e95dfba0b676d4327c8b6fca252b6c0f2be6 |