Django removing object from ManyToMany relationship

前端 未结 3 656
遇见更好的自我
遇见更好的自我 2020-12-12 16:47

How would I delete an object from a Many-to-Many relationship without removing the actual object?

Example:

I have the models Moods

3条回答
  •  盖世英雄少女心
    2020-12-12 17:36

    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()
    

提交回复
热议问题