[Django]-"invalid literal for int() with base 10:" But can't see why it is accessing an integer

5👍

Consider the line

ea = Event_Attendance(request.POST['talk'], member)

You are creating an instance of the model Event_Attendance. The constructor expects the first argument to be an instance of Talk. Instead you are supplying it with a string which is the title of the talk.

A quick fix would be to look up the Talk instance with the matching title and pass it as an argument to the constructor. Something like this:

talk = Talk.objects.get(title = request.POST['talk'])
ea = Event_Attendance(talk = talk, membersAttended = member)
ea.save()

This will work but is not the best way to go about it. For one, the first line can always raise a DoesNotExist if a Talk with the given title is not found. Second you are accessing the POST variables directly rather than using the Form to validate them.

A better answer would therefore be:

eventAttendForm = EventAttendForm(request.POST.copy())
if eventAttendForm.is_valid():
    talk = eventAttendForm.cleaned_data['talk']
    membersAttended = eventAttendForm.cleaned_data['membersAttended']

    for member in membersAttended:
        ea = Event_Attendance(talk = talk, membersAttended = member)
        ea.save()

Leave a comment