elo80ka / django-dynamic-formset

A jQuery plugin that allows you dynamically add new forms to a rendered django formset.
677 stars 311 forks source link

Dynamically added row cannot be saved, deleted row still be saved #161

Open HPJason opened 4 years ago

HPJason commented 4 years ago

I am using django 3.0.2.

  1. The row can be added visually for inlineformset in the website, however, the row dynamically added cannot save to database.
  2. The row removed from the inlineformset is just hidden and can still be sent to the database, I don't want this to happen.

Any idea to fix it, thanks.

a-wip0 commented 3 years ago

it still happen in django 3.1

fbzyx commented 3 years ago

Hello. I have the same problem? Did you find a solution?

vitorfs commented 3 years ago

I'm currently only using it for inline formsets within tables

I managed to fix the deletion problem by editing the source code, updating this part of code:

if (del.length) {
    // We're dealing with an inline formset.
    // Rather than remove this form from the DOM, we'll mark it as deleted
    // and hide it, then let Django handle the deleting:
    del.val('on');
    row.hide();
    forms = $('.' + options.formCssClass).not(':hidden');
    totalForms.val(forms.length);
}

To this:

if (del.length) {
    // We're dealing with an inline formset.
    // Rather than remove this form from the DOM, we'll mark it as deleted
    // and hide it, then let Django handle the deleting:
    del.val('on');
    row.hide();
    forms = $('.' + options.formCssClass);
    totalForms.val(forms.length);
}

I was getting a weird behavior when deleting several items because by excluding the hidden rows (marked as deleted) from the totalForms count, Django's formset apis wasn't processing the deletion properly.

sabitrakhanal commented 3 years ago

Having same issue with Django 1.11 and above changes is not helping. @vitorfs seems like we should not change totalForms count on deletion.