Dynamically remove inline forms in Django

Is it possible that Django will automatically delete forms that are not in the request?

So, for example, if I had three built-in form sets presented in HTML when I loaded my edit page, and I use javascript to remove two of them when the request is processed. Django sees that these two forms are no longer them and delete them.

+7
django django-forms
source share
1 answer

Yes, this is possible using several different methods.

First you need to copy the method that he performed in the Django admin application, which should have a flag with a label similar to "Delete?". Then, when you process the form set later in the POST request, you check to see if the True check box is selected, and if so, delete the entry. This is probably not what you are looking for since you used the word โ€œdynamicallyโ€ in the title of the question :)

So, the second, dynamic method would be to use Javascript to โ€œhideโ€ the deleted entry on the page and set the delete flag to True. Then you process the set of forms in the same way as the first option above. See django-dynamic-formset for removing form code this way.

The third, dynamic way is to use Ajax, and when the delete button is clicked, JS will call delete to delete the entry, as well as remove the form set from the HTML. I cannot point you to any sample code for this, but I think the second method above is better anyway, because you can save all your authentication and standard form verification code in one place.

+8
source share

All Articles