summaryrefslogtreecommitdiff
path: root/tests/m2m_intermediary
diff options
context:
space:
mode:
authorFlorian Apolloner <florian@apolloner.eu>2013-02-26 09:53:47 +0100
committerFlorian Apolloner <florian@apolloner.eu>2013-02-26 14:36:57 +0100
commit89f40e36246100df6a11316c31a76712ebc6c501 (patch)
tree6e65639683ddaf2027908d1ecb1739e0e2ff853b /tests/m2m_intermediary
parentb3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff)
downloaddjango-89f40e36246100df6a11316c31a76712ebc6c501.tar.gz
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/m2m_intermediary')
-rw-r--r--tests/m2m_intermediary/__init__.py0
-rw-r--r--tests/m2m_intermediary/models.py42
-rw-r--r--tests/m2m_intermediary/tests.py41
3 files changed, 83 insertions, 0 deletions
diff --git a/tests/m2m_intermediary/__init__.py b/tests/m2m_intermediary/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/m2m_intermediary/__init__.py
diff --git a/tests/m2m_intermediary/models.py b/tests/m2m_intermediary/models.py
new file mode 100644
index 0000000000..e9ae422afb
--- /dev/null
+++ b/tests/m2m_intermediary/models.py
@@ -0,0 +1,42 @@
+"""
+9. Many-to-many relationships via an intermediary table
+
+For many-to-many relationships that need extra fields on the intermediary
+table, use an intermediary model.
+
+In this example, an ``Article`` can have multiple ``Reporter`` objects, and
+each ``Article``-``Reporter`` combination (a ``Writer``) has a ``position``
+field, which specifies the ``Reporter``'s position for the given article
+(e.g. "Staff writer").
+"""
+from __future__ import unicode_literals
+
+from django.db import models
+from django.utils.encoding import python_2_unicode_compatible
+
+
+@python_2_unicode_compatible
+class Reporter(models.Model):
+ first_name = models.CharField(max_length=30)
+ last_name = models.CharField(max_length=30)
+
+ def __str__(self):
+ return "%s %s" % (self.first_name, self.last_name)
+
+@python_2_unicode_compatible
+class Article(models.Model):
+ headline = models.CharField(max_length=100)
+ pub_date = models.DateField()
+
+ def __str__(self):
+ return self.headline
+
+@python_2_unicode_compatible
+class Writer(models.Model):
+ reporter = models.ForeignKey(Reporter)
+ article = models.ForeignKey(Article)
+ position = models.CharField(max_length=100)
+
+ def __str__(self):
+ return '%s (%s)' % (self.reporter, self.position)
+
diff --git a/tests/m2m_intermediary/tests.py b/tests/m2m_intermediary/tests.py
new file mode 100644
index 0000000000..f261f23546
--- /dev/null
+++ b/tests/m2m_intermediary/tests.py
@@ -0,0 +1,41 @@
+from __future__ import absolute_import
+
+from datetime import datetime
+
+from django.test import TestCase
+from django.utils import six
+
+from .models import Reporter, Article, Writer
+
+
+class M2MIntermediaryTests(TestCase):
+ def test_intermeiary(self):
+ r1 = Reporter.objects.create(first_name="John", last_name="Smith")
+ r2 = Reporter.objects.create(first_name="Jane", last_name="Doe")
+
+ a = Article.objects.create(
+ headline="This is a test", pub_date=datetime(2005, 7, 27)
+ )
+
+ w1 = Writer.objects.create(reporter=r1, article=a, position="Main writer")
+ w2 = Writer.objects.create(reporter=r2, article=a, position="Contributor")
+
+ self.assertQuerysetEqual(
+ a.writer_set.select_related().order_by("-position"), [
+ ("John Smith", "Main writer"),
+ ("Jane Doe", "Contributor"),
+ ],
+ lambda w: (six.text_type(w.reporter), w.position)
+ )
+ self.assertEqual(w1.reporter, r1)
+ self.assertEqual(w2.reporter, r2)
+
+ self.assertEqual(w1.article, a)
+ self.assertEqual(w2.article, a)
+
+ self.assertQuerysetEqual(
+ r1.writer_set.all(), [
+ ("John Smith", "Main writer")
+ ],
+ lambda w: (six.text_type(w.reporter), w.position)
+ )