Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django. Invalid keyword argument for this function. ManyToMany

Tags:

django

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)

I try:

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!

like image 838
sagem_tetra Avatar asked Oct 30 '12 16:10

sagem_tetra


1 Answers

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:

Django ManyToMany field is not created in model

@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.

like image 75
Timmy O'Mahony Avatar answered Oct 01 '22 12:10

Timmy O'Mahony