diff options
Diffstat (limited to 'tests/m2o_recursive')
-rw-r--r-- | tests/m2o_recursive/__init__.py | 0 | ||||
-rw-r--r-- | tests/m2o_recursive/models.py | 32 | ||||
-rw-r--r-- | tests/m2o_recursive/tests.py | 42 |
3 files changed, 74 insertions, 0 deletions
diff --git a/tests/m2o_recursive/__init__.py b/tests/m2o_recursive/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/m2o_recursive/__init__.py diff --git a/tests/m2o_recursive/models.py b/tests/m2o_recursive/models.py new file mode 100644 index 0000000000..2775d713dd --- /dev/null +++ b/tests/m2o_recursive/models.py @@ -0,0 +1,32 @@ +""" +11. Relating an object to itself, many-to-one + +To define a many-to-one relationship between a model and itself, use +``ForeignKey('self')``. + +In this example, a ``Category`` is related to itself. That is, each +``Category`` has a parent ``Category``. + +Set ``related_name`` to designate what the reverse relationship is called. +""" + +from django.db import models +from django.utils.encoding import python_2_unicode_compatible + + +@python_2_unicode_compatible +class Category(models.Model): + name = models.CharField(max_length=20) + parent = models.ForeignKey('self', blank=True, null=True, related_name='child_set') + + def __str__(self): + return self.name + +@python_2_unicode_compatible +class Person(models.Model): + full_name = models.CharField(max_length=20) + mother = models.ForeignKey('self', null=True, related_name='mothers_child_set') + father = models.ForeignKey('self', null=True, related_name='fathers_child_set') + + def __str__(self): + return self.full_name diff --git a/tests/m2o_recursive/tests.py b/tests/m2o_recursive/tests.py new file mode 100644 index 0000000000..fa04c74cca --- /dev/null +++ b/tests/m2o_recursive/tests.py @@ -0,0 +1,42 @@ +from __future__ import absolute_import + +from django.test import TestCase + +from .models import Category, Person + + +class ManyToOneRecursiveTests(TestCase): + + def setUp(self): + self.r = Category(id=None, name='Root category', parent=None) + self.r.save() + self.c = Category(id=None, name='Child category', parent=self.r) + self.c.save() + + def test_m2o_recursive(self): + self.assertQuerysetEqual(self.r.child_set.all(), + ['<Category: Child category>']) + self.assertEqual(self.r.child_set.get(name__startswith='Child').id, self.c.id) + self.assertEqual(self.r.parent, None) + self.assertQuerysetEqual(self.c.child_set.all(), []) + self.assertEqual(self.c.parent.id, self.r.id) + +class MultipleManyToOneRecursiveTests(TestCase): + + def setUp(self): + self.dad = Person(full_name='John Smith Senior', mother=None, father=None) + self.dad.save() + self.mom = Person(full_name='Jane Smith', mother=None, father=None) + self.mom.save() + self.kid = Person(full_name='John Smith Junior', mother=self.mom, father=self.dad) + self.kid.save() + + def test_m2o_recursive2(self): + self.assertEqual(self.kid.mother.id, self.mom.id) + self.assertEqual(self.kid.father.id, self.dad.id) + self.assertQuerysetEqual(self.dad.fathers_child_set.all(), + ['<Person: John Smith Junior>']) + self.assertQuerysetEqual(self.mom.mothers_child_set.all(), + ['<Person: John Smith Junior>']) + self.assertQuerysetEqual(self.kid.mothers_child_set.all(), []) + self.assertQuerysetEqual(self.kid.fathers_child_set.all(), []) |