Removing a form from a django form set

I am trying to implement a django form set (where the user can dynamically add / remove forms from a form set).

I use JS to add new lines (using empty_form):

$("#add-item").click(function(e){ e.preventDefault(); var count = parseInt($('#id_form-TOTAL_FORMS').val()); $('.invoice-items').append($('#empty_invoice_item').html().replace(/__prefix__/g, count)); $('#id_form-TOTAL_FORMS').attr('value', count+1); $(".invoice-items .invoice-item .col-lg-9 .form-group:last-child").last().append('<a href="#" class="delete-item"><i class="glyphicon glyphicon-remove"></i></a>') }); 

I also use JS to set the DELETE flag for certain forms. Everything is submitted to the submission.

My view code (part):

 invoice_form = InvoiceForm() invoice_item_helper = InvoiceItemHelper InvoiceItemFormset = formset_factory(InvoiceItemForm, extra=0, max_num=15, validate_max=True, min_num=1, validate_min=True, can_delete=True) formset = InvoiceItemFormset() if request.method == 'POST': invoice_form = InvoiceForm(request.POST) formset = InvoiceItemFormset(request.POST) 

The problem is that django always displays all forms in the form set, even those that are marked for deletion. Thus, even in my invoice form, something is wrong and it is not verified, it will display an invoice form with an error message and all forms (again).

How to delete all forms marked for deletion in the if request.method == 'POST': block? Is it possible?

+5
source share
2 answers

It depends on how you render the forms, but you can check the form.DELETE field in the template, and if it is set, display this form, which is hidden for display, and the data will be transferred until the data has been processed (when all other forms are valid). It also ensures that prefix and index forms for a set of forms are not corrupted.

When the format is verified, it will ignore the forms marked for deletion. formset.is_valid

You can also choose which forms will be deleted in the view using deleted_forms and possibly process them, anyway you will have to rebuild the entire formet without deleted forms to maintain indexes and number of forms. I personally found out that this is complicated and leads to complex code.

+6
source

Django provides a removal function that should ensure that your form is deleted correctly: https://docs.djangoproject.com/en/1.8/topics/forms/formsets/#can-delete

The value of form-X-DELETE should be set to some value evaluating to true. The default is on , as you can see in the example from the documentation:

 >>> data = { ... 'form-TOTAL_FORMS': '3', ... 'form-INITIAL_FORMS': '2', ... 'form-MAX_NUM_FORMS': '', ... 'form-0-title': 'Article #1', ... 'form-0-pub_date': '2008-05-10', ... 'form-0-DELETE': 'on', ... 'form-1-title': 'Article #2', ... 'form-1-pub_date': '2008-05-11', ... 'form-1-DELETE': '', ... 'form-2-title': '', ... 'form-2-pub_date': '', ... 'form-2-DELETE': '', ... } 

So, if this does not work, maybe you are not actually sending the value to your form-X-DELETE ? Please note that you need to provide the actual form data when submitting the deletion. Simply submitting the form number is not enough to identify Django.

+1
source

All Articles