About Initialising Custom Widgets for Admin Inlines

In Django administration, you can initialise your widgets for inlines while listening to the custom jQuery formset:added event as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
django.jQuery(function($) {
  function initRichtextWidgets($container) {
    $container.find('.richtext:not([name*="__prefix__"])').each(
      function() {
        // init the rich-text widget for $(this)
      }
    );
  }

  $(document).on('formset:added', function(event, $row, prefix) {
    initRichtextWidgets($row);
  });

  initRichtextWidgets($(document.body));
});

Since Django 4.1, formset:added is a custom native event. So you would use it like this

1
2
3
4
document.addEventListener('formset:added', (event) => {
    let $row = $(event.target);
    initRichtextWidgets($row);    
});

User Experience Web Design Django 5.x Django 4.2 Django 3.2 JavaScript