Fixed #7539, #13067 -- Added on_delete argument to ForeignKey to control cascade behavior. Also refactored deletion for efficiency and code clarity. Many thanks to Johannes Dollinger and Michael Glassford for extensive work on the patch, and to Alex Gaynor, Russell Keith-Magee, and Jacob Kaplan-Moss for review.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14507 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Carl Meyer 2010-11-09 16:46:42 +00:00
parent 3ba3294c6b
commit 616b30227d
28 changed files with 850 additions and 608 deletions

View file

@ -6,7 +6,7 @@ from django.contrib.admin.util import display_for_field, label_for_field, lookup
from django.contrib.admin.util import NestedObjects
from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
from django.contrib.sites.models import Site
from django.db import models
from django.db import models, DEFAULT_DB_ALIAS
from django.test import TestCase
from django.utils import unittest
from django.utils.formats import localize
@ -20,51 +20,50 @@ class NestedObjectsTests(TestCase):
"""
def setUp(self):
self.n = NestedObjects()
self.n = NestedObjects(using=DEFAULT_DB_ALIAS)
self.objs = [Count.objects.create(num=i) for i in range(5)]
def _check(self, target):
self.assertEquals(self.n.nested(lambda obj: obj.num), target)
def _add(self, obj, parent=None):
# don't bother providing the extra args that NestedObjects ignores
self.n.add(None, None, obj, None, parent)
def _connect(self, i, j):
self.objs[i].parent = self.objs[j]
self.objs[i].save()
def _collect(self, *indices):
self.n.collect([self.objs[i] for i in indices])
def test_unrelated_roots(self):
self._add(self.objs[0])
self._add(self.objs[1])
self._add(self.objs[2], self.objs[1])
self._connect(2, 1)
self._collect(0)
self._collect(1)
self._check([0, 1, [2]])
def test_siblings(self):
self._add(self.objs[0])
self._add(self.objs[1], self.objs[0])
self._add(self.objs[2], self.objs[0])
self._connect(1, 0)
self._connect(2, 0)
self._collect(0)
self._check([0, [1, 2]])
def test_duplicate_instances(self):
self._add(self.objs[0])
self._add(self.objs[1])
dupe = Count.objects.get(num=1)
self._add(dupe, self.objs[0])
self._check([0, 1])
def test_non_added_parent(self):
self._add(self.objs[0], self.objs[1])
self._connect(0, 1)
self._collect(0)
self._check([0])
def test_cyclic(self):
self._add(self.objs[0], self.objs[2])
self._add(self.objs[1], self.objs[0])
self._add(self.objs[2], self.objs[1])
self._add(self.objs[0], self.objs[2])
self._connect(0, 2)
self._connect(1, 0)
self._connect(2, 1)
self._collect(0)
self._check([0, [1, [2]]])
def test_queries(self):
self._connect(1, 0)
self._connect(2, 0)
# 1 query to fetch all children of 0 (1 and 2)
# 1 query to fetch all children of 1 and 2 (none)
# Should not require additional queries to populate the nested graph.
self.assertNumQueries(2, self._collect, 0)
class UtilTests(unittest.TestCase):
def test_values_from_lookup_field(self):