Deleting form from django formset

It's dependent on how you render the forms, but you can check the field form.DELETE in the template and if it's set, render that form hidden for display and the data will be passed along until the data is processed (when all other forms are valid). It will also make sure form prefixes and indexes for the formset is intact.

When a formset is validated it will ignore forms marked for deletion. formset.is_valid

You can also pick up which forms are deleted in the view using deleted_forms and perhaps process them, still you will have to rebuild the whole formset without the deleted forms to maintain indexes and the count of forms. I personally found out that doing that is complex and leads to complicated code.


Django provides a for-deletion feature which should enable proper deletion of your form: https://docs.djangoproject.com/en/1.8/topics/forms/formsets/#can-delete

The form-X-DELETE needs to be set to some value evaluating to true though. 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 that is not working, perhaps you are not actually sending a value to your form-X-DELETE? Do note that you need to specify the actual form data when sending the delete. Simply sending the form number alone is not enough identification for Django to accept it.