Description

A django.forms.widgets.Widget can add javascript in its render function. This can include state variables related to the widget.

This problem applies to widgets in inline forms. When an extra inline is added (using javascript in admin), a special hidden empty inline is cloned, setup and showed. The widget javascript was applied to the hidden inline, so any state is connected to that.

What is needed is to not run the javascript for the empty inline's widget, and to trigger the javascript to run on any added (cloned) inline forms' widget. I suggest we modify django/contrib/admin/static/admin/js/inlines.js to trigger a custom event that contains the added DOM element. The widget code can then bind its activation function to that event, and apply it to the widget HTML within the event's element.