How do you remove a manytomany relationship in django?

6,575 views
Skip to first unread message

Ray

unread,
Aug 18, 2009, 4:49:55 PM8/18/09
to Django users
I know in the backend, a manytomany relationship is essentially a join
table, but there seems to be no easy way to delete relationships from
it. The only remove option is made only for ForeignKey relationships.
Is there a workaround? Why would django design a relationship that you
can only add but not delete?

Daniel Roseman

unread,
Aug 18, 2009, 7:02:41 PM8/18/09
to Django users
The wording in the documentation here:
http://docs.djangoproject.com/en/dev/ref/models/relations/
is potentially confusing. The top of the page clearly states that the
methods are available both for foreignkeys and many-to-many
relationships. However, remove() and clear() state that they are "only
available on ForeignKeys where null=True". I believe this should just
read "for FKs, these methods are only available where null=True" - in
other words, they are available as you would expect for ManyToMany.
--
DR.

Daniel Roseman

unread,
Aug 18, 2009, 7:03:14 PM8/18/09
to Django users
On Aug 18, 9:49 pm, Ray <raymend...@gmail.com> wrote:

Ray

unread,
Aug 19, 2009, 5:09:16 PM8/19/09
to Django users
Hi,
thanks for the fast response! I'm trying to use the remove function
right now just like this sample code:

# And from the other end
>>> p2.article_set.remove(a5)
>>> p2.article_set.all()
[]
>>> a5.publications.all()
[]

from http://www.djangoproject.com/documentation/models/many_to_many/

I'm doing "Group.objects.get(id=3).members.remove(member1)"

and getting this error: 'ManyRelatedManager' object has no attribute
'remove'

I also did help to find out what methods ManyRelatedManager had, and
indeed it didn't have a 'remove' method, only: clear, create,
get_or_create, and get_query_set

AND THEN,

I tried the remove function with the queryset object,
"Group.objects.get(id=3).members.all().remove(member1)"

and I got this error: 'QuerySet' object has no attribute 'remove'

I'm at a loss as to how I'm supposed to update these relationships....

Ray

Daniel Roseman

unread,
Aug 19, 2009, 5:30:00 PM8/19/09
to Django users
On Aug 19, 10:09 pm, Ray <raymend...@gmail.com> wrote:
> Hi,
> thanks for the fast response! I'm trying to use the remove function
> right now just like this sample code:
>
> # And from the other end>>> p2.article_set.remove(a5)
> >>> p2.article_set.all()
> []
> >>> a5.publications.all()
>
> []
>
> fromhttp://www.djangoproject.com/documentation/models/many_to_many/
>
> I'm doing "Group.objects.get(id=3).members.remove(member1)"
>
> and getting this error: 'ManyRelatedManager' object has no attribute
> 'remove'
>
> I also did help to find out what methods ManyRelatedManager had, and
> indeed it didn't have a 'remove' method, only: clear, create,
> get_or_create, and get_query_set
>
> AND THEN,
>
> I tried the remove function with the queryset object,
> "Group.objects.get(id=3).members.all().remove(member1)"
>
> and I got this error: 'QuerySet' object has no attribute 'remove'
>
> I'm at a loss as to how I'm supposed to update these relationships....
>
> Ray

Well, that is weird. MayRelatedManager definitely *does* have an
attribute 'remove'. With these basic models:

class Category(models.Model):
title = models.CharField(_('title'), max_length=100)

class Post(models.Model):
title = models.CharField(_('title'), max_length=200)
categories = models.ManyToManyField(Category)


the following works fine:
>>> from blog.models import Post, Category
>>> p=Post.objects.create(title='dan')
>>> c=Category.objects.create(title='category')
>>> p.categories.all()
[]
>>> p.categories.add(c)
>>> p.categories.all()
[<Category: category>]
>>> p.categories.remove(c)
>>> p.categories.all()
[]

So it works for me, I can't imagine what is happening with your setup.
What version of Django are you using?
--
DR.
Reply all
Reply to author
Forward
0 new messages