Django. Invalid keyword argument for this function. ManyToMany
I have this error:
'people' is an invalid keyword argument for this function
class Passage(models.Model): name= models.CharField(max_length = 255) who = models.ForeignKey(UserProfil) class UserPassage(models.Model): passage = models.ForeignKey(Passage) people = models.ManyToManyField(UserProfil, null=True) class UserProfil(models.Model): user = models.OneToOneField(User) name = models.CharField(max_length=50)
def join(request): user = request.user user_profil = UserProfil.objects.get(user=user) passage = Passage.objects.get(id=2) #line with error up = UserPassage.objects.create(people= user_profil, passage=passage) return render_to_response('thanks.html')
How does one do this correctly? Thanks!
You need to save/create the object before you can add ManyToMany relationships:
up = UserPassage.objects.create(passage=passage) up.people.add(user_profil)
ManyToMany relationships aren't saved as columns in your table. Read the first reply here for good explanation:
@DanielRoseman: Because a ManyToMany isn't a field, at least not one that exists as a database column. It's a relationship with a linking table. You'll find that a table named myapp_teacher_subjects has been created, with foreign keys to both teacher and subjects.