I wonder whether you have to save a record to the database BEFORE you
can update a ManyToMany field?

I came across a post by Karen Tracey that seems to suggest this.

The problem is that I am unable to do so because the ManyToManyField
in my model has no default value and this triggers an error when I try
to save without adding a value to it. And when I try to add a value
django says that the "instance needs a primary key value before a many-
to-many relationship can be used." Thus, it's kind of a trap, isn't
it?

I am working on an app with similar models to the document "Making
queries" at
http://docs.djangoproject.com/en/1.0/topics/db/queries

only that I change the authors-field with a users field pointing to
the auth.model User.

class Entry(models.Model):
    [..]
    users = models.ManyToManyField(User)

(KT's post below)

Please advise.

Robert



[The] way to update a ManyToMany field is to call the add() method on
the
many-to-many field, not to assign anything to it.  So I'd think what
you
need to do is:

         if f.is_valid():
           newgroup = f.save(commit=False)
           newgroup.group_owner = request.user
           newgroup.save()
           newgroup.group_members.add(request.user)
           newgroup.group_admins.add(request.user)
           return HttpResponseRedirect("/group/list/")


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to