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)

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!

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.


Need Your Help

Calling 32bit Code from 64bit Process

.net migration x86 64bit fortran

I have an application that we're trying to migrate to 64bit from 32bit. It's .NET, compiled using the x64 flags. However, we have a large number of DLLs written in FORTRAN 90 compiled for 32bit. ...

Difference between Static methods and Instance methods

java static static-methods non-static

I was just reading over the text given to me in my textbook and I'm not really sure I understand what it is saying. It's basically telling me that static methods or class methods include the "modif...