[Django]-How to remove all many to many objects in Django

3👍

i think foo.moo.clear() should work

6👍

Use the clear method.

Removes all objects from the related object set:

b = Blog.objects.get(id=1)
b.entry_set.clear() 

Note this doesn’t delete the related objects – it just disassociates them.

Just like remove(), clear() is only available on ForeignKeys where
null=True.

0👍

use the related objects clear method clear clear() to dissociate all Foo related objects

moo.foo_set.clear()

to delete objects use delete()queryset method

Foo.objects.all().delete()

you can delete by one object using filter() default manager method

Foo.objects.filter(name='moo1').delete()

Leave a comment