Skip to content

Commit

Permalink
Fixed django#14896 -- Ensured that _meta.get_all_related_objects(incl…
Browse files Browse the repository at this point in the history
…ude_hidden=True) also works correctly with model inheritance.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15248 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
carljm committed Jan 19, 2011
1 parent 7790d27 commit 70ae70e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion django/db/models/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def _fill_related_objects_cache(self):
cache = SortedDict()
parent_list = self.get_parent_list()
for parent in self.parents:
for obj, model in parent._meta.get_all_related_objects_with_model():
for obj, model in parent._meta.get_all_related_objects_with_model(include_hidden=True):
if (obj.field.creation_counter < 0 or obj.field.rel.parent_link) and obj.model not in parent_list:
continue
if not model:
Expand Down
9 changes: 9 additions & 0 deletions tests/regressiontests/delete_regress/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ class PlayedWith(models.Model):
class PlayedWithNote(models.Model):
played = models.ForeignKey(PlayedWith)
note = models.TextField()

class Contact(models.Model):
label = models.CharField(max_length=100)

class Email(Contact):
email_address = models.EmailField(max_length=100)

class Researcher(models.Model):
contacts = models.ManyToManyField(Contact, related_name="research_contacts")
43 changes: 27 additions & 16 deletions tests/regressiontests/delete_regress/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from django.db import backend, connection, transaction, DEFAULT_DB_ALIAS
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature

from models import Book, Award, AwardNote, Person, Child, Toy, PlayedWith, PlayedWithNote
from models import (Book, Award, AwardNote, Person, Child, Toy, PlayedWith,
PlayedWithNote, Contact, Email, Researcher)


# Can't run this test under SQLite, because you can't
Expand Down Expand Up @@ -62,17 +63,12 @@ def test_concurrent_delete(self):
transaction.commit()
self.assertEqual(1, Book.objects.count())


class DeleteCascadeTests(TestCase):
def test_generic_relation_cascade(self):
"""
Test that Django cascades deletes through generic-related
objects to their reverse relations.
This might falsely succeed if the database cascades deletes
itself immediately; the postgresql_psycopg2 backend does not
give such a false success because ForeignKeys are created with
DEFERRABLE INITIALLY DEFERRED, so its internal cascade is
delayed until transaction commit.
Django cascades deletes through generic-related objects to their
reverse relations.
"""
person = Person.objects.create(name='Nelson Mandela')
Expand All @@ -87,13 +83,10 @@ def test_generic_relation_cascade(self):

def test_fk_to_m2m_through(self):
"""
Test that if a M2M relationship has an explicitly-specified
through model, and some other model has an FK to that through
model, deletion is cascaded from one of the participants in
the M2M, to the through model, to its related model.
Like the above test, this could in theory falsely succeed if
the DB cascades deletes itself immediately.
If an M2M relationship has an explicitly-specified through model, and
some other model has an FK to that through model, deletion is cascaded
from one of the participants in the M2M, to the through model, to its
related model.
"""
juan = Child.objects.create(name='Juan')
Expand All @@ -108,6 +101,24 @@ def test_fk_to_m2m_through(self):
# first two asserts just sanity checks, this is the kicker:
self.assertEquals(PlayedWithNote.objects.count(), 0)


class DeleteCascadeTransactionTests(TransactionTestCase):
def test_inheritance(self):
"""
Auto-created many-to-many through tables referencing a parent model are
correctly found by the delete cascade when a child of that parent is
deleted.
Refs #14896.
"""
r = Researcher.objects.create()
email = Email.objects.create(
label="office-email", email_address="carl@science.edu"
)
r.contacts.add(email)

email.delete()

class LargeDeleteTests(TestCase):
def test_large_deletes(self):
"Regression for #13309 -- if the number of objects > chunk size, deletion still occurs"
Expand Down

0 comments on commit 70ae70e

Please sign in to comment.