theatlantic / django-nested-admin

Django admin classes that allow for nested inlines
http://django-nested-admin.readthedocs.org/
Other
690 stars 97 forks source link

Bug: Autocomplete does not work when adding a nested inline #237

Open beeekey opened 1 year ago

beeekey commented 1 year ago

I have an Inline model as following:

class SomeModelInline(InlinePermissionsMixin, nested_admin.NestedStackedInline):
    model = MyModel
    extra = 1
    autocomplete_fields = ['type']
    form = MyModelForm

When setting extra=1 the autocomplete works for the first entry (works for all which are defined with extra) , when adding another inline entry by clicking the + add button, the autocomplete field is empty. Comparing the html of these two elements, there are a lot of html tags missing.

I am using Django 4.2

beeekey commented 1 year ago

My solution so far:

// here we setup a global event listener for the `formset:added` event (which is setup in `static\admin\js\autocomplete.js`)
// event to get triggered when adding a new django inline formset (by clicking the plus button below the inline formset)

(function($) {
    $(document).ready(function() {
        var els = document.getElementsByClassName("djn-add-handler");

        const formUpdateEvent = new CustomEvent("formset:added", { "detail": "Dummy event" });

        for (el of els) {
            el.addEventListener("click", function () {

                    setTimeout(function(){
                        document.dispatchEvent(formUpdateEvent);
                    }, 250);

            });
        }
    });
})(django.jQuery);