Skip to content

Commit 2da6773

Browse files
authored
Update admin.py
1 parent 508f6c3 commit 2da6773

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

admin.py

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from django.contrib import admin
22
from django.forms.models import modelform_factory
33
from django.utils.translation import gettext_lazy as _
4+
from django.forms import widgets
5+
from django.utils.safestring import mark_safe
6+
from django.db import models
47

58
# Imports for Dynamic app registrations
69
from django.apps import apps
@@ -20,7 +23,74 @@
2023
global_app_name = 'web' # Replace '' with your app name
2124

2225

23-
# ADMIN.PY MAIN CODE
26+
27+
28+
# Json Editor Widget
29+
# https://github.com/json-editor/json-editor
30+
class JsonEditorWidget(widgets.Widget):
31+
template_name = 'json_editor_widget.html'
32+
33+
def __init__(self, schema, *args, **kwargs):
34+
super().__init__(*args, **kwargs)
35+
self.schema = schema
36+
37+
def render(self, name, value, attrs=None, renderer=None):
38+
# Convert Python dictionary to JSON string
39+
json_value = value if value else '{}'
40+
# Render the JSON Editor
41+
style = '''
42+
.form-row {
43+
overflow: visible !important;
44+
}
45+
.je-switcher{
46+
margin-left: 0px !important;
47+
}
48+
.je-ready button{
49+
padding: 5px 10px !important;
50+
border: 1px solid grey !important;
51+
}
52+
.je-ready button i{
53+
margin-right: 5px !important;
54+
margin-left: 5px !important;
55+
}
56+
.je-indented-panel{
57+
border-radius: 0px !important;
58+
padding: 20px 15px !important;
59+
}
60+
'''
61+
return mark_safe(f'''
62+
63+
<!-- FontAwesome CSS -->
64+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
65+
66+
<!-- JSON Editor CSS -->
67+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.css">
68+
69+
<!-- JSON Editor JS -->
70+
<script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
71+
72+
<style>{style}</style>
73+
<script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
74+
<textarea name="{name}" id="tx_{attrs['id']}" style="display:none;">{json_value}</textarea>
75+
<div id="editor_{attrs['id']}"></div>
76+
<script>
77+
document.addEventListener("DOMContentLoaded", function() {{
78+
var editor = new JSONEditor(document.getElementById("editor_{attrs['id']}"), {{
79+
schema: {self.schema},
80+
startval: {json_value},
81+
theme: 'html',
82+
iconlib: 'fontawesome5',
83+
}});
84+
editor.on('change', function() {{
85+
console.log("Change event triggered");
86+
console.log("Editor Value:", editor.getValue());
87+
document.getElementById("tx_{attrs['id']}").value = JSON.stringify(editor.getValue());
88+
}});
89+
}});
90+
</script>
91+
''')
92+
93+
2494
class GenericStackedAdmin(admin.StackedInline):
2595
extra = 1
2696
# This method ensures the field order is correct for inlines as well
@@ -106,6 +176,23 @@ def get_fieldsets(self, request, obj=None):
106176

107177
return fieldsets
108178

179+
def formfield_for_dbfield(self, db_field, request, **kwargs):
180+
# Check if the field is a JSONField
181+
if isinstance(db_field, models.JSONField) and self.admin_meta:
182+
# Retrieve the schema configuration for JSON fields
183+
json_fields_meta = self.model.admin_meta.get('json_fields', {})
184+
185+
# Retrieve the schema for the specific field, if defined
186+
json_schema = json_fields_meta.get(db_field.name, {}).get('schema')
187+
188+
if json_schema:
189+
# Initialize the custom widget with the specified schema
190+
kwargs['widget'] = JsonEditorWidget(schema=json_schema)
191+
# else: # Patch this later
192+
# # Else load the django-jsoneditor widget
193+
# kwargs['widget'] = JSONEditor()
194+
195+
return super().formfield_for_dbfield(db_field, request, **kwargs)
109196

110197
def get_readonly_fields(self, request, obj=None):
111198
# Get a list of non-editable fields

0 commit comments

Comments
 (0)