abogushov / django-admin-json-editor

Adds json-editor for JSONField in Django Administration
MIT License
175 stars 61 forks source link

Django admin inlines support (and js code separation from template) #23

Open alpianon opened 5 years ago

alpianon commented 5 years ago

I needed django admin inlines support, and I realized that it could be achieved only by moving django_admin_json_editor javascript code outside the template (editor.html) to a separate js file.

When using inlines in admin, Django code creates an empty (and hidden) div in the HTML DOM with a "dummy" id (like id_mymodel_set-__prefix__-fieldname) which is used as a template inline row editor. Such template row is dinamically copied and given a "real" id (like id_mymodel_set-0-fieldname) only when user clicks to add a row.

In such case, the js code embedded in the template editor.html would instantiate JSONEditor on the hidden "dummy" div element with the dummy id, so when such dummy element is copied, JSONEditor would seem to work but it could not save data because the div element id has changed.

So I ended up adding a handler for the "formset:added" event in a separate js file (see here) in order to instantiate JSONEditor only when the "real" div element is dinamically added via javascript.

At this point, it came natural to move all django_admin_json_editor's js code from editor.html to a separate js file and instantiate JSONEditor always via event handlers (after page is loaded, and after a row is added by user)

I also modified the example in order to show how it works.

wsantos commented 5 years ago

@abogushov any plan to merge this ? I need this feature too.