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

Deleting instance not working when hideLastAddForm is true #197

Open
corneliusabel opened this issue Jan 27, 2022 · 1 comment
Open

Deleting instance not working when hideLastAddForm is true #197

corneliusabel opened this issue Jan 27, 2022 · 1 comment

Comments

@corneliusabel
Copy link

When using inline formsets (extra=1, can_delete=True) and hideLastAddForm=true the deletion of an instance (after submitting) fails. However, with hideLastAddForm=false deletion works.
I noticed that the only difference in the POST requests was the "TOTAL_FORMS" field which was n-1 when hideLastAddForm=true. That has prevented the deletion of the object in django.
By commenting out line "totalForms.val(forms.length);" in line 95 (1.5-pre) i could fix that for me.

However, iam not an javascript expert so not sure if this is the right solution?

Best,

Cornelius

@ridoo
Copy link

ridoo commented Apr 30, 2024

My observation is rather that TOTAL_FORMS counter is decremented on delete. See

forms = $('.' + options.formCssClass).not(':hidden');
totalForms.val(forms.length);

and

// Update the TOTAL_FORMS count:
forms = $('.' + options.formCssClass).not('.formset-custom-template');
totalForms.val(forms.length);

For what I traced in the Django code is, that the TOTAL_FORMS count has to include both visible and hidden form groups in the formset, as all form data is POSTed (deleted ones are marked with DELETE: "on" attribute). Decreasing the counter would not make the formset creating any (initial) forms for data having an index bigger than the decreased TOTAL_FORMS .

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

2 participants