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

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

Open
beeekey opened this issue Jun 28, 2023 · 1 comment
Open

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

beeekey opened this issue Jun 28, 2023 · 1 comment

Comments

@beeekey
Copy link

beeekey commented Jun 28, 2023

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

beeekey commented Jun 28, 2023

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);

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

No branches or pull requests

1 participant