1👍
To be honest, I would do this with javascript. Maybe this is not considered the best way, or actually the best way, but nonetheless it is a way.
Template:
<head>
<script>
checkbox=getelementbyId('#container');
var itemlist= [];
function togglelist(newitem){
field=getelementbyId('#'+newitem);
if (!field.value){
itemlist.push(newitem)
}
else{
itemlist.remove(itemlist.indexOf(newitem))
};
checkbox.innerHTML="<input type='hidden' id='checkbox' label='hidden_field' value='"+String(itemlist)+"'>";
}
</script>
</head>
<body>
<div id="container"> </div>
{% for m in messages %}
<input type="checkbox" id="{{m.id}}" onchange="togglelist({{m.id}})">
{{m.body}}
{% endfor %}
Your models.py you can get rid of the boolean value and just have one character field, hidden_field.
Then in your views.py just parse the list from the string, using ast library:
import ast
form = CheckBoxForm()
if request.method =='POST' and form.is_valid():
deletelist = request.POST.get('hidden_field', False)
ids=ast.literal_eval(deletelist)
for id in ids:
Message.objects.filter(pk=id).delete()
Sorry I wasn’t able to test this, but I hope it is helpful.
Source:stackexchange.com