Django admin inlines support (and js code separation from template) #23
+180
−28
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.