Dynamically Delete inline formsets in Django

2020-03-13 08:47发布

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.

1条回答
家丑人穷心不美
2楼-- · 2020-03-13 09:33

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.

查看更多
登录 后发表回答