Good evening,
I’m hoping someone knows an answer to my problem of getting all checked rows inside a table into a multidimensional list (after submit)?
I know I can do something like this to get – for example – all the checked ids into a list:
template:
JavaScript
x
25
25
1
<form action="" method="post">
2
{% csrf_token %}
3
4
<div class="table-wrapper">
5
<table class="table table-hover">
6
<thead>
7
<tr>
8
<th scope="col"></th>
9
<th scope="col">id</th>
10
<th scope="col">country</th>
11
<th scope="col">continent</th>
12
<th scope="col"></th>
13
</tr>
14
</thead>
15
<tbody>
16
{% for item in data %}
17
<tr>
18
<td>
19
<input type="checkbox" name="list_gap" value="{{ item.id }}">
20
</td>
21
<td>{{ item.id}}</td>
22
<td>{{ item.country }}</td>
23
<td>{{ item.continent}}</td>
24
<td></td>
25
views.py
JavaScript
1
3
1
if request.method == 'POST':
2
checked_data = request.POST.getlist('list_gap')
3
But as mentioned, this will only get me a list of the checked ids! Is there a way to get all the data of the row into a multidimensional list, for example like..?
JavaScript
1
7
1
list_checked = [
2
[1, 'country', 'continent', '...'],
3
[2, 'country', 'continent', '...'],
4
[3, 'country', 'continent', '...'],
5
6
]
7
Thanks for all your help and a good night!
Advertisement
Answer
I’d recommend creating a queryset to pull the instances from the DB.
JavaScript
1
3
1
checked_data = request.POST.getlist('list_gap')
2
checked_instances = Model.objects.filter(id__in=checked_data)
3