Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

alpianon
Copy link

@alpianon alpianon commented Dec 5, 2018

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
Copy link

wsantos commented May 17, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants