0

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),
3
  • what error are you getting? Commented Jan 24, 2020 at 7:49
  • Field 'id' expected a number but got <Delegate: Delegate object (14)>. Commented Jan 24, 2020 at 7:55
  • @ruddra i know i have to add a for loop to insert multiple data rows but i am unable to figure out how and where to use it in the view method Commented Jan 24, 2020 at 7:58

1 Answer 1

1

Well from comments what I understand is that, you are receiving value of regdelegate properly. But as the error said, you are using the Delegate object, where it expects an integer. You can solve this problem by either:

EventDelegate.objects.create(event_id=eventid, delegate=delid)

OR

EventDelegate.objects.create(event_id=eventid, delegate_id=tosave[0])

Finally, you can use a for loop to iterate and create EventDelegate object:

for delid in tosave:  # maybe use a better variable name then tosave to improve code readability
    EventDelegate.objects.create(event_id=eventid, delegate_id=delid)
Sign up to request clarification or add additional context in comments.

1 Comment

i got this solution on my mind before you answered and already implemented it and working like a charm. still accepting for your efforts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.