Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Delete inline formsets in Django

Is it possible to have Django automatically delete formsets that are not present in the request?

So for example if I had three inline formsets represented in HTML when I loaded my edit page and I use javascript to remove two of those when the request is processes Django sees that those two forms are no longer their and deletes them.

like image 395
BenMills Avatar asked Mar 01 '10 15:03

BenMills


1 Answers

Yes, it's possible using a few different methods.

First is to copy the way it's done in the Django admin app, which is to have a checkbox with a label similar to "Delete?". Then, when you are processing the formset later in the POST request, you check to see if the checkbox is True and if so, delete the record. This probably isn't what you are looking for though since you used the word "dynamically" in your question title :)

So a second, dynamic, method would be to use Javascript to "hide" the deleted record on the page and set the delete checkbox to True. Then you process the formset the same way as the first option above. See django-dynamic-formset for code to delete formset this way.

The third, dynamic, way would be to use Ajax and when the delete button is clicked have JS call a delete view to delete the record and also delete the formset from the HTML. I can't point you to any example code for this, but I think the second method above is better anyway because you can keep all your authentication and standard form validation code in one place.

like image 87
Van Gale Avatar answered Sep 23 '22 08:09

Van Gale