|
1 | 1 | [](https://badge.fury.io/py/django-svelte-jsoneditor)
|
2 |
| -[](https://django-svelte-jsoneditor.readthedocs.io/en/latest/?badge=latest) |
| 2 | +[](https://django-svelte-jsoneditor.readthedocs.io/en/latest/) |
3 | 3 | [](https://github.com/psf/black)
|
4 | 4 | [](https://github.com/pre-commit/pre-commit)
|
| 5 | +[](https://github.com/octue/django-svelte-jsoneditor/actions/workflows/codeql.yml) |
5 | 6 |
|
6 | 7 | # django-svelte-jsoneditor
|
7 | 8 |
|
8 | 9 | A plug in widget for django's JSONField that allows manipulation and display of JSON data. The widget is built using Jos deJong's new [svelte-jsoneditor](https://github.com/josdejong/svelte-jsoneditor).
|
9 | 10 |
|
10 |
| -This app is a replacement for `django-jsoneditor` (which uses a deprecated version of the widget, `jsoneditor` - you can [see the differences here](https://github.com/josdejong/svelte-jsoneditor#differences-between-josdejongsvelte-jsoneditor-and-josdejongjsoneditor)). You can read about why we're not directly contributing to `django-jsoneditor` [here](https://github.com/nnseva/django-jsoneditor/issues/71), the two projects might merge in the future. |
11 |
| - |
12 |
| -## Documentation |
13 |
| - |
14 |
| -### Installation |
15 |
| - |
16 |
| -To get an application installed in your project, you need to add `django_svelte_jsoneditor` into `INSTALLED_APPS` in `settings.py` file. |
17 |
| - |
18 |
| -```python |
19 |
| -# settings.py |
20 |
| - |
21 |
| -INSTALLED_APPS = [ |
22 |
| - # Other Django applications |
23 |
| - "django_svelte_jsoneditor", |
24 |
| -] |
25 |
| -``` |
26 |
| - |
27 |
| -### Usage |
28 |
| - |
29 |
| -The application contains new widget called `SvelteJSONEditorWidget` which adds editor capabilities to JSON fields in Django. Below you can see an example, of how to override the default widget for the JSON field (in this case textarea widget). |
30 |
| - |
31 |
| -```python |
32 |
| -# admin.py |
33 |
| - |
34 |
| -from django.contrib import admin |
35 |
| -from django_svelte_jsoneditor.widgets import SvelteJSONEditorWidget |
36 |
| - |
37 |
| -from .models import MyModel |
38 |
| - |
39 |
| - |
40 |
| -@admin.register(MyModel) |
41 |
| -class MyModelAdmin(admin.ModelAdmin): |
42 |
| - formfield_overrides = { |
43 |
| - models.JSONField: { |
44 |
| - "widget": SvelteJSONEditorWidget, |
45 |
| - } |
46 |
| - } |
47 |
| -``` |
48 |
| - |
49 |
| -Another example is how to create a new Django form integrating `SvelteJSONEditorWidget` replacing `Textarea` widget. |
50 |
| - |
51 |
| -```python |
52 |
| -# forms.py |
53 |
| -from django import forms |
54 |
| -from django_svelte_jsoneditor.widgets import SvelteJSONEditorWidget |
55 |
| - |
56 |
| - |
57 |
| -class MyJSONEditorForm(forms.Form): |
58 |
| - json = forms.JSONField(widgets=SvelteJSONEditorWidget()) |
59 |
| -``` |
60 |
| - |
61 |
| -### Global settings |
62 |
| - |
63 |
| -The application allows modifying some properties of svelte-jsoneditor inside the settings.py configuration file in Django. Official documentation is available on [svelte-jsoneditor](https://github.com/josdejong/svelte-jsoneditor#properties) GitHub page. **Note:** Not all options are configurable which are provided by svelte-jsoneditor. |
64 |
| - |
65 |
| -```python |
66 |
| -# settings.py |
67 |
| - |
68 |
| -SVELTE_JSONEDITOR = { |
69 |
| - "mode": "tree", |
70 |
| - "mainMenuBar": True, |
71 |
| - "navigationBar": True, |
72 |
| - "statusBar": True, |
73 |
| - "askToFormat": True, |
74 |
| - "readOnly": False, |
75 |
| - "indentations": 4, |
76 |
| - "tabSize": 4, |
77 |
| - "escapeControlCharacters": False, |
78 |
| - "flattenColumns": True, |
79 |
| -} |
80 |
| -``` |
81 |
| - |
82 |
| -#### Available properties |
83 |
| - |
84 |
| -| Property | Type | Default | |
85 |
| -| ----------------------- | --------------------------- | ------- | |
86 |
| -| mode | 'tree' or 'text' or 'table' | 'tree' | |
87 |
| -| mainMenuBar | boolean | True | |
88 |
| -| navigationBar | boolean | True | |
89 |
| -| statusBar | boolean | True | |
90 |
| -| askToFormat | boolean | True | |
91 |
| -| readOnly | boolean | False | |
92 |
| -| indentations | number or string | 4 | |
93 |
| -| tabSize | number | 4 | |
94 |
| -| escapeControlCharacters | boolean | False | |
95 |
| -| flattenColumns | boolean | True | |
96 |
| - |
97 |
| -### Widget properties |
98 |
| - |
99 |
| -`SvelteJSONEditorWidget` has additional argument called `props` which allows to override `SVELTE_JSONEDITOR` settings from settings.py. |
100 |
| - |
101 |
| -```python |
102 |
| -# forms.py |
103 |
| - |
104 |
| -from django import forms |
105 |
| - |
106 |
| - |
107 |
| -class SvelteJsonEditorForm(forms.Form): |
108 |
| - my_json = forms.JSONField(widget=SvelteJSONEditorWidget(props={ |
109 |
| - "readOnly": True |
110 |
| - })) |
111 |
| -``` |
112 |
| - |
113 |
| -#### Custom widget properties in admin form |
114 |
| - |
115 |
| -```python |
116 |
| -# admin.py |
117 |
| - |
118 |
| -from django import forms |
119 |
| -from django.contrib import admin |
120 |
| - |
121 |
| -from .models import ExampleModel |
122 |
| - |
123 |
| -class CustomForm(forms.ModelForm): |
124 |
| - class Meta: |
125 |
| - model = ExampleModel |
126 |
| - fields = "__all__" |
127 |
| - |
128 |
| - def __init__(self, *args, **kwargs): |
129 |
| - super().__init__(*args, **kwargs) |
130 |
| - self.fields["some_json_field"].widget = SvelteJSONEditorWidget(props={"readOnly": True}) |
131 |
| - |
132 |
| - |
133 |
| -@admin.register(ExampleModel, ExampleModelAdmin) |
134 |
| -class ExampleModelAdmin(admin.ModelAdmin): |
135 |
| - form = CustomForm |
136 |
| - formfield_overrides = { |
137 |
| - models.JSONField: { |
138 |
| - "widget": SvelteJSONEditorWidget, |
139 |
| - } |
140 |
| - } |
141 |
| -``` |
142 |
| - |
143 |
| -## About Svelte |
144 |
| - |
145 |
| -**You don't need to know or care.** It's the JavaScript framework used to develop the widget - but the widget JS is all pre-built so there are no extra requirements. |
146 |
| - |
147 |
| -### Developing |
148 |
| - |
149 |
| -To get started with developing `django-svelte-jsoneditor`, fork the repo then open an environment in the devcontainer (the easiest way is to use GitHub codespaces or VSCode remote containers), then type: |
150 |
| - |
151 |
| -``` |
152 |
| -python manage.py migrate |
153 |
| -python manage.py createsuperuser |
154 |
| -# (then enter user details for yourself) |
155 |
| -python manage.py runserver |
156 |
| -# (then go to the localhost address in your browser) |
157 |
| -# (and in another terminal...) |
158 |
| -pytest |
159 |
| -# (this should run all tests and have them pass) |
160 |
| -``` |
161 |
| - |
162 |
| -You'll find this takes you to the django admin where you have several example models registered, each of which use slightly different options on the json field, so you can see how the widget behaves. |
163 |
| - |
164 |
| -**On the subject of commit messages**. It's helpful if you use the following [conventional commit codes](https://github.com/octue/conventional-commits#default-allowed-commit-codes) because then the PR and release notes get generated automatically! |
| 11 | +[**Read the documentation here.**](https://django-svelte-jsoneditor.readthedocs.io/en/latest/) |
0 commit comments