Skip to content

Commit 175aa09

Browse files
committed
Example
1 parent 103cc3f commit 175aa09

File tree

17 files changed

+333
-17
lines changed

17 files changed

+333
-17
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Django Administration JSON Editor
2+
3+
Application adds support for editing JSONField in Django Administration via https://github.com/jdorn/json-editor.
4+
5+
## Quick start
6+
7+
Install application via pip:
8+
9+
```bash
10+
pip install git+https://github.com/abogushov/django-admin-json-editor
11+
```
12+
13+
Add application to the INSTALLED_APPS settings:
14+
15+
```python
16+
INSTALLED_APPS = [
17+
...
18+
'json_editor',
19+
...
20+
]
21+
```
22+
23+
Define schema of json field:
24+
25+
```python
26+
DATA_SCHEMA = {
27+
'type': 'object',
28+
'title': 'Data',
29+
'properties': {
30+
'text': {
31+
'title': 'Some text',
32+
'type': 'string',
33+
'format': 'textarea',
34+
},
35+
'status': {
36+
'title': 'Status',
37+
'type': 'boolean',
38+
},
39+
},
40+
}
41+
```
42+
43+
Use JSONEditorWidget to bind editor to the form field:
44+
45+
```python
46+
class JSONModelAdminForm(forms.ModelForm):
47+
class Meta:
48+
model = JSONModel
49+
fields = '__all__'
50+
widgets = {
51+
'data': JSONEditorWidget(DATA_SCHEMA, collapsed=False),
52+
}
53+
```

README.rst

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
db.sqlite3
2+
env/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# An example of using application
2+
3+
To run example to this steps:
4+
5+
```bash
6+
python3 -m venv env
7+
. env/bin/activate
8+
pip install -r requirements.txt
9+
createdb json_editor_example
10+
python manage.py migrate
11+
python manage.py createsuperuser
12+
python manage.py runserver
13+
```
14+
15+
Open `http://localhost:8000/admin/app/jsonmodel/add/`. You will see:
16+
17+
![Example](django-admin-json-editor/examples/json_editor_example/example.png)
18+

examples/json_editor_example/app/__init__.py

Whitespace-only changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from django.contrib import admin
2+
from django import forms
3+
4+
from json_editor import JSONEditorWidget
5+
6+
from .models import JSONModel
7+
8+
9+
DATA_SCHEMA = {
10+
'type': 'object',
11+
'title': 'Data',
12+
'properties': {
13+
'text': {
14+
'title': 'Some text',
15+
'type': 'string',
16+
'format': 'textarea',
17+
},
18+
'status': {
19+
'title': 'Status',
20+
'type': 'boolean',
21+
},
22+
},
23+
}
24+
25+
26+
class JSONModelAdminForm(forms.ModelForm):
27+
class Meta:
28+
model = JSONModel
29+
fields = '__all__'
30+
widgets = {
31+
'data': JSONEditorWidget(DATA_SCHEMA, collapsed=False),
32+
}
33+
34+
35+
@admin.register(JSONModel)
36+
class JSONModelAdmin(admin.ModelAdmin):
37+
form = JSONModelAdminForm
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AppConfig(AppConfig):
5+
name = 'app'
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.2 on 2017-06-13 04:23
3+
from __future__ import unicode_literals
4+
5+
import django.contrib.postgres.fields.jsonb
6+
from django.db import migrations, models
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
initial = True
12+
13+
dependencies = [
14+
]
15+
16+
operations = [
17+
migrations.CreateModel(
18+
name='JSONModel',
19+
fields=[
20+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
21+
('data', django.contrib.postgres.fields.jsonb.JSONField(default={'status': False, 'text': 'some text'})),
22+
],
23+
),
24+
]

examples/json_editor_example/app/migrations/__init__.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.db import models
2+
from django.contrib.postgres.fields import JSONField
3+
4+
class JSONModel(models.Model):
5+
data = JSONField(default={
6+
'text': 'some text',
7+
'status': False,
8+
})

0 commit comments

Comments
 (0)