I can’t see why in my application i am getting this error. As all primary keys are text fields are integers. Here is my code:
forms.py
class EventAttendForm(forms.Form):
talk = forms.ModelChoiceField(queryset=Talk.objects.all())
membersAttended = forms.ModelMultipleChoiceField(queryset=Member.objects.all())
models.py
class Talk(models.Model):
title = models.CharField(max_length=200, primary_key=True)
class Member(models.Model):
name = models.CharField(max_length=200)
membership_number = models.CharField(max_length=200, primary_key=True)
talks = models.ManyToManyField(Talk, through='Event_Attendance')
class Event_Attendance(models.Model):
talk = models.ForeignKey('Talk')
membersAttended = models.ForeignKey('Member')
views.py
def addAttendance(request):
#eventAttendanceForm
if request.method == 'POST':
eventAttendForm = EventAttendForm(request.POST)
for member in request.POST['membersAttended']:
ea = Event_Attendance(request.POST['talk'], member)
ea.save()
return HttpResponseRedirect('../../../talks/')
else:
eventAttendForm = EventAttendForm()
return render_to_response('attendance/addAttendance.html',{'eventAttendForm': eventAttendForm})
I cannot see why I am getting this error. The exact error is:
Exception Type: ValueError
Exception Value: invalid literal for int() with base 10: 'redirectTest'
RedirectTest is a Talk that I created with my form for the add talk to check that redirection was working correctly. The line of code that it is saying is causing the problem is ea.save().
Consider the line
You are creating an instance of the model
Event_Attendance. The constructor expects the first argument to be an instance ofTalk. Instead you are supplying it with a string which is the title of the talk.A quick fix would be to look up the
Talkinstance with the matching title and pass it as an argument to the constructor. Something like this:This will work but is not the best way to go about it. For one, the first line can always raise a
DoesNotExistif aTalkwith the given title is not found. Second you are accessing thePOSTvariables directly rather than using theFormto validate them.A better answer would therefore be: