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:

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

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