I want to insert some data into a many to many field. I ‘m getting this Error
user is an invalid keyword argument for this function
i also tried it with the relatedName…but still is not working…
My model looks like this:
models.py
class Workspace(models.Model):
user = models.ManyToManyField(User,null=False, blank=False, related_name='members')
workspace_name = models.CharField(max_length=80, null=False, blank=False)
workspace_cat =models.CharField(max_length=80, null=True, blank=True)
views.py
db= Workspace(user=5, workspace_name=data_to_db['workspace_name'],workspace_cat=data_to_db['workspace_category'])
db.save()
Does somebody has an idea? Thanks a lot!
You used a
ManyToManyfield for theuserfield of yourWorkspaceobject, you can’t give it one user, that’s not how aManyToManyworks, that would be aForeignKey.Basically, using a
ForeignKey, each workspace has oneUserassociated to it, there’s a direct linkWorkspace -> User, so it makes sense to create aWorkspaceand pass it anUser, like you would be filling in aCharField.A
ManyToManyrelationship means that several users can be associated to aWorkspaceand severalWorkspacesto oneUser. When using aManyToMany, you would create yourWorkspaceand then add someUsers to it.To add to a
ManyToManyrelationship, do the following:You should rename the
userfield tousersto make the relationship name clearer.