diff --git a/django/core/meta/__init__.py b/django/core/meta/__init__.py index 31a131d29d..52572053f1 100644 --- a/django/core/meta/__init__.py +++ b/django/core/meta/__init__.py @@ -775,6 +775,9 @@ def method_delete(opts, self): for rel_opts, rel_field in opts.get_all_related_many_to_many_objects(): cursor.execute("DELETE FROM %s WHERE %s_id=%%s" % (rel_field.get_m2m_db_table(rel_opts), self._meta.object_name.lower()), [getattr(self, opts.pk.name)]) + for f in opts.many_to_many: + cursor.execute("DELETE FROM %s WHERE %s_id=%%s" % (f.get_m2m_db_table(opts), self._meta.object_name.lower()), + [getattr(self, opts.pk.name)]) cursor.execute("DELETE FROM %s WHERE %s=%%s" % (opts.db_table, opts.pk.name), [getattr(self, opts.pk.name)]) db.db.commit() setattr(self, opts.pk.name, None) diff --git a/tests/testapp/models/many_to_many.py b/tests/testapp/models/many_to_many.py index 9b38b515b4..283a004ca4 100644 --- a/tests/testapp/models/many_to_many.py +++ b/tests/testapp/models/many_to_many.py @@ -69,11 +69,18 @@ True >>> p1.get_article_list(order_by=['headline']) [Django lets you build Web apps easily, NASA uses Python] -# If we delete an article, its publication won't be able to access it. +# If we delete a Publication, its Articles won't be able to access it. +>>> p1.delete() +>>> publications.get_list() +[Science News] +>>> a1 = articles.get_object(pk=1) +>>> a1.get_publication_list() +[] + +# If we delete an Article, its Publications won't be able to access it. >>> a2.delete() >>> articles.get_list() [Django lets you build Web apps easily] >>> p1.get_article_list(order_by=['headline']) [Django lets you build Web apps easily] - """