django Cannot set values on a ManyToManyField which specifies an intermediary model. Use Manager instead
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.
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.
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.