[Django]-How to delete an instance of a ManyToMany field in Django

5👍

To delete ManyToMany relationships, use remove() instead of delete.

act.attendee.remove(attendee)

Also i suggest to change the naming conventions for better readability,

class Activity(models.Model):
    attendees = models.ManyToManyField(Attendee, related_name="activities", null=True, blank=True)

So removing logic will be,

act.attendees.remove(attendee)

You can also remove using the reverse relationship,

attendee.activities.remove(act)

Leave a comment