Basically I have three tables in my models:
1) Delegates - storing all delegates
2) Events - storing all events
3) EventDelegate - relation between delegate and event (which delegate is registered for which event)
The delegates table is displayed in a template of each event that also contains the event and i hav put a checkbox beside each row to save the checked row and send the ids of all checked rows to the view function so that i can insert the ids of delegates with loop into the EventDelegate table along with the same eventid (it will remain same for each row because i am sending data from each event page).
So finally i should be able to check the delegates from the list of all for each event page and clicking on submit will add their IDs along with the event id to the EventDelegate Table. I have did something but is not working the way it should, sharing my work till now:-
models.py
...
class EventDelegate(models.Model):
event = models.ForeignKey(Event, on_delete=models.DO_NOTHING)
delegate = models.ForeignKey(Delegate, on_delete=models.DO_NOTHING)
template.html
<form action="/register-delegates" method="POST">
{% csrf_token %}
<table id="myTable2" class="table table-striped table-bordered" style="width:100%">
<thead class="thead-light">
<tr>
<th scope="col"></th>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Phone</th>
<th scope="col">Company</th>
<th scope="col">Designation</th>
<th></th>
</tr>
</thead>
<tbody>
{% for del in delegates %}
<tr>
//CHECKBOX TO SELECT MULTIPLE DELEGATES <td><input type="checkbox" name="regdelegate" value="{{ del.id }}"></td>
<td>{{ del.id }}</td>
<td>{{ del.first_name }} {{ del.last_name }}</td>
<td>{{ del.email }}</td>
<td>{{ del.phone }}</td>
<td>{{ del.company }}</td>
<td>{{ del.designation }}</td>
<td><a href="{% url 'delegate' dataset_id=del.dataset_id %}">View</a></td>
</tr>
{% endfor %}
//STORING THE EVENT ID HERE FOR POSTING <input type="hidden" name="eventid" value="{{ ev.id }}" />
<input type="submit" name="register" value="Register Selected Delegates" />
</tbody>
</table>
</form>
views.py
def register_delegates(request):
if request.method=='POST':
eventid = request.POST.get('eventid')
tosave = request.POST.getlist('regdelegate')
delid = Delegate.objects.get(id=tosave[0])
EventDelegate.objects.create(event_id=eventid,delegate_id=delid)
return HttpResponseRedirect('/view-events')
urls.py
...
path('register-delegates', views.register_delegates),