Django Cannot set values on a ManyToManyField which specifies an intermediary model
- by dana
i am using a m2m and a through table, and when i was trying to save, my error was:
Cannot set values on a ManyToManyField which specifies an intermediary model
so, i've modified my code, so that when i save the form, to insert data into the 'through' table too.But now, i'm having another error.
(i've bolded the lines where i think i am wrong)
i have in models.py:
class Classroom(models.Model):
user = models.ForeignKey(User, related_name = 'classroom_creator')
classname = models.CharField(max_length=140, unique = True)
date = models.DateTimeField(auto_now=True)
open_class = models.BooleanField(default=True)
members = models.ManyToManyField(User,related_name="list of invited members", through = 'Membership')
class Membership(models.Model):
accept = models.BooleanField(User)
date = models.DateTimeField(auto_now = True)
classroom = models.ForeignKey(Classroom, related_name = 'classroom_membership')
member = models.ForeignKey(User, related_name = 'user_membership')
and in
def save_classroom(request):
if request.method == 'POST':
form = ClassroomForm(request.POST, request.FILES, user = request.user)
**classroom_instance = Classroom
member_instance = Membership**
if form.is_valid():
new_obj = form.save(commit=False)
new_obj.user = request.user
r = Relations.objects.filter(initiated_by = request.user)
membership = Membership.objects.create(**classroom = classroom_instance, member = member_instance,date=datetime.datetime.now())**
new_obj.save()
form.save_m2m()
return HttpResponseRedirect('/classroom/classroom_view/{{user}}/')
else:
form = ClassroomForm(user = request.user)
return render_to_response('classroom/classroom_form.html', {
'form': form,
},
context_instance=RequestContext(request))
but i don't seem to initialise okay the classroom_instance and menber_instance.My error os:
Cannot assign "": "Membership.classroom" must be a "Classroom" instance.
Thanks!