Dynamically Delete inline formsets in Django

后端 未结 1 1810
说谎
说谎 2021-01-12 06:29

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 wh

相关标签:
1条回答
  • 2021-01-12 06:52

    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.

    0 讨论(0)
提交回复
热议问题