Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'RelatedManager' object has no attribute 'remove'

Somewhere in my code it says

publisher.publisherperson_set.remove(email__in=pp_remove_set)

Upon executing this, I got

AttributeError: 'RelatedManager' object has no attribute 'remove'

And indeed: I looked at dir(publisher.publisherperson_set) and it had plenty of operations (including add), but not remove.

Under which circumstances is this possible?

like image 237
Lutz Prechelt Avatar asked Sep 12 '17 13:09

Lutz Prechelt


2 Answers

Cannot be null

The documentation for RelatedManager.remove() says

For ForeignKey objects, this method only exists if null=True. If the related field can’t be set to None (NULL), then an object can’t be removed from a relation without being added to another.

Obvious, once you think about it.
What I really intended to do was this:

publisher.publisherperson_set.filter(email__in=pp_remove_set).delete()
like image 81
Lutz Prechelt Avatar answered Oct 20 '22 16:10

Lutz Prechelt


Just posting this since I got here from a related search AttributeError: 'RelatedManager' object has no attribute 'delete'

What I was looking for was:

thing.stuff_set.all().delete()

note: still learning django, but i'm assuming any queryset operations filter, all, order_by, exclude, annotate, etc

  • https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.ordered

can be used. Although I'm not sure at the moment what sort of nuance there is in regards to the queryset list return from all() and the stuff_set:

  • Does Django queryset values_list return a list object?
like image 11
jmunsch Avatar answered Oct 20 '22 18:10

jmunsch