291👍
✅
my_mood.interests.remove(my_interest)
Note: you might have to get an instance of my_mood
and my_interest
using Django’s QuerySet API before you can execute this code.
125👍
If you need to remove all M2M references without touching the underlying objects, it’s easier to work from the other direction:
interest.mood_set.clear()
While this does not directly address the OP’s question, it’s often useful in this situation.
- [Django]-How to get getting base_url in django template
- [Django]-How to get the currently logged in user's id in Django?
- [Django]-Django-rest-framework returning 403 response on POST, PUT, DELETE despite AllowAny permissions
24👍
In your case you can simply clear the relationship
my_mood.interests.clear()
Then perhaps when you are again creating new relation in your serializer you can do something like this
interests = Interests.objects.get_or_create(name='Something')
my_mood_obj.tags.add(tag[0])
my_mood_obj.save()
- [Django]-Django-allauth social account connect to existing account on login
- [Django]-Having Django serve downloadable files
- [Django]-How to put comments in Django templates?
-1👍
model.field.remove(object_you_want_to_remove)
In this case use: my_mood.interests.remove(my_interest)
- [Django]-Django. A good tutorial for Class Based Views
- [Django]-Getting Django admin url for an object
- [Django]-Django FileField upload is not working for me
Source:stackexchange.com