django Cannot set values on a ManyToManyField whic

2019-01-11 23:09发布

i am working on saving on the same form two tables - having a m2m relation. I don't succeed, my error persists with something like: Cannot set values on a ManyToManyField which specifies an intermediary model. Use Membership's Manager instead where Membership is my 'through table'.

my code :

def save_classroom(request):
   classroom_instance = Classroom()
   if request.method == 'POST':
        form = ClassroomForm(request.POST, request.FILES, user = request.user) 
        if form.is_valid():
           new_obj = form.save(commit=False)
           new_obj.user = request.user 
           new_obj.save()
           membership = Membership(member = request.user,classroom=new_obj)
           membership.save() 
           form.save_m2m()
           return HttpResponseRedirect('.')    
   else:
           form = ClassroomForm(user = request.user)     
   return render_to_response('classroom/classroom_form.html', {
           'form': form,

           }, 
          context_instance=RequestContext(request))  

my models:

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(default=False)
      date = models.DateTimeField(auto_now = True) 
      classroom = models.ForeignKey(Classroom, related_name = 'classroom_membership')
      member = models.ForeignKey(User, related_name = 'user_membership')

where am i wrong?

标签: django forms m2m
3条回答
别忘想泡老子
2楼-- · 2019-01-11 23:30

As seen on:

http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany

Unlike normal many-to-many fields, you can't use add, create, or assignment (i.e., beatles.members = [...]) to create relationships

I guess your code trips up on the line "form.save_m2m()", which is unnecessary since you already manually create a membership.

查看更多
来,给爷笑一个
3楼-- · 2019-01-11 23:38

I had a similar error message on a different problem. I post it here just in case it helps others.

I've added a new ManyToManyField on an existing model. This model was used in a ModelForm built with an exclude field.

I fixed the problem by add the new field in the excluded ones.

查看更多
地球回转人心会变
4楼-- · 2019-01-11 23:56

If you are allowed to modify class Membership, adding auto_created = True might solve your problem,

class Membership(models.Model): 
    class Meta:
        auto_created = True

In Django 1.7, the error message is changed to "Cannot set values on a ManyToManyField which specifies an intermediary model". The solution is the same.

NOTE: This will remove your intermediate model entirely, and all the additional fields with it.

查看更多
登录 后发表回答