summaryrefslogtreecommitdiff
path: root/tests/modeltests/m2m_multiple/models.py
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2006-05-02 01:31:56 +0000
committerAdrian Holovaty <adrian@holovaty.com>2006-05-02 01:31:56 +0000
commitf69cf70ed813a8cd7e1f963a14ae39103e8d5265 (patch)
treed3b32e84cd66573b3833ddf662af020f8ef2f7a8 /tests/modeltests/m2m_multiple/models.py
parentd5dbeaa9be359a4c794885c2e9f1b5a7e5e51fb8 (diff)
MERGED MAGIC-REMOVAL BRANCH TO TRUNK. This change is highly backwards-incompatible. Please read http://code.djangoproject.com/wiki/RemovingTheMagic for upgrade instructions.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@2809 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/modeltests/m2m_multiple/models.py')
-rw-r--r--tests/modeltests/m2m_multiple/models.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/tests/modeltests/m2m_multiple/models.py b/tests/modeltests/m2m_multiple/models.py
new file mode 100644
index 0000000000..3fec427c1d
--- /dev/null
+++ b/tests/modeltests/m2m_multiple/models.py
@@ -0,0 +1,79 @@
+"""
+20. Multiple many-to-many relationships between the same two tables
+
+In this example, an Article can have many Categories (as "primary") and many
+Categories (as "secondary").
+
+Set ``related_name`` to designate what the reverse relationship is called.
+"""
+
+from django.db import models
+
+class Category(models.Model):
+ name = models.CharField(maxlength=20)
+ class Meta:
+ ordering = ('name',)
+
+ def __repr__(self):
+ return self.name
+
+class Article(models.Model):
+ headline = models.CharField(maxlength=50)
+ pub_date = models.DateTimeField()
+ primary_categories = models.ManyToManyField(Category, related_name='primary_article_set')
+ secondary_categories = models.ManyToManyField(Category, related_name='secondary_article_set')
+ class Meta:
+ ordering = ('pub_date',)
+
+ def __repr__(self):
+ return self.headline
+
+API_TESTS = """
+>>> from datetime import datetime
+
+>>> c1 = Category(name='Sports')
+>>> c1.save()
+>>> c2 = Category(name='News')
+>>> c2.save()
+>>> c3 = Category(name='Crime')
+>>> c3.save()
+>>> c4 = Category(name='Life')
+>>> c4.save()
+
+>>> a1 = Article(headline='Area man steals', pub_date=datetime(2005, 11, 27))
+>>> a1.save()
+>>> a1.primary_categories.add(c2, c3)
+>>> a1.secondary_categories.add(c4)
+
+>>> a2 = Article(headline='Area man runs', pub_date=datetime(2005, 11, 28))
+>>> a2.save()
+>>> a2.primary_categories.add(c1, c2)
+>>> a2.secondary_categories.add(c4)
+
+>>> a1.primary_categories.all()
+[Crime, News]
+
+>>> a2.primary_categories.all()
+[News, Sports]
+
+>>> a1.secondary_categories.all()
+[Life]
+
+
+>>> c1.primary_article_set.all()
+[Area man runs]
+>>> c1.secondary_article_set.all()
+[]
+>>> c2.primary_article_set.all()
+[Area man steals, Area man runs]
+>>> c2.secondary_article_set.all()
+[]
+>>> c3.primary_article_set.all()
+[Area man steals]
+>>> c3.secondary_article_set.all()
+[]
+>>> c4.primary_article_set.all()
+[]
+>>> c4.secondary_article_set.all()
+[Area man steals, Area man runs]
+"""