summaryrefslogtreecommitdiff
path: root/tests/testapp
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2005-12-01 06:01:46 +0000
committerAdrian Holovaty <adrian@holovaty.com>2005-12-01 06:01:46 +0000
commitfa9cbe5ea01cbb82efdb2780c8381189b294f39c (patch)
treef0a0b910bc6344a22f46f935af31e2513f909670 /tests/testapp
parent9abc0120035be34f56c57f7d6d5989798f693d45 (diff)
Added unit tests to confirm #452. Refs #452.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@1515 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/testapp')
-rw-r--r--tests/testapp/models/__init__.py2
-rw-r--r--tests/testapp/models/m2m_multiple.py99
2 files changed, 100 insertions, 1 deletions
diff --git a/tests/testapp/models/__init__.py b/tests/testapp/models/__init__.py
index a8aeada597..a5a41035d6 100644
--- a/tests/testapp/models/__init__.py
+++ b/tests/testapp/models/__init__.py
@@ -2,4 +2,4 @@ __all__ = ['basic', 'repr', 'custom_methods', 'many_to_one', 'many_to_many',
'ordering', 'lookup', 'get_latest', 'm2m_intermediary', 'one_to_one',
'm2o_recursive', 'm2o_recursive2', 'save_delete_hooks', 'custom_pk',
'subclassing', 'many_to_one_null', 'custom_columns', 'reserved_names',
- 'or_lookups']
+ 'or_lookups', 'm2m_multiple']
diff --git a/tests/testapp/models/m2m_multiple.py b/tests/testapp/models/m2m_multiple.py
new file mode 100644
index 0000000000..d8793acb73
--- /dev/null
+++ b/tests/testapp/models/m2m_multiple.py
@@ -0,0 +1,99 @@
+"""
+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.
+
+Set ``singular`` to designate what the category object is called. This is
+required if a model has multiple ``ManyToManyFields`` to the same object.
+"""
+
+from django.core import meta
+
+class Category(meta.Model):
+ name = meta.CharField(maxlength=20)
+ class META:
+ module_name = 'categories'
+ ordering = ('name',)
+
+ def __repr__(self):
+ return self.name
+
+class Article(meta.Model):
+ headline = meta.CharField(maxlength=50)
+ pub_date = meta.DateTimeField()
+ primary_categories = meta.ManyToManyField(Category,
+ singular='primary_category', related_name='primary_article')
+ secondary_categories = meta.ManyToManyField(Category,
+ singular='secondary_category', related_name='secondary_article')
+ class META:
+ ordering = ('pub_date',)
+
+ def __repr__(self):
+ return self.headline
+
+API_TESTS = """
+>>> from datetime import datetime
+
+>>> c1 = categories.Category(name='Sports')
+>>> c1.save()
+>>> c2 = categories.Category(name='News')
+>>> c2.save()
+>>> c3 = categories.Category(name='Crime')
+>>> c3.save()
+>>> c4 = categories.Category(name='Life')
+>>> c4.save()
+
+>>> a1 = articles.Article(headline='Area man steals', pub_date=datetime(2005, 11, 27))
+>>> a1.save()
+>>> a1.set_primary_categories([c2.id, c3.id])
+True
+>>> a1.set_secondary_categories([c4.id])
+True
+
+>>> a2 = articles.Article(headline='Area man runs', pub_date=datetime(2005, 11, 28))
+>>> a2.save()
+>>> a2.set_primary_categories([c1.id, c2.id])
+True
+>>> a2.set_secondary_categories([c4.id])
+True
+
+# The "primary_category" here comes from the "singular" parameter. If we hadn't
+# specified the "singular" parameter, Django would just use "category", which
+# would cause a conflict because the "primary_categories" and
+# "secondary_categories" fields both relate to Category.
+>>> a1.get_primary_category_list()
+[Crime, News]
+
+# Ditto for the "primary_category" here.
+>>> a2.get_primary_category_list()
+[News, Sports]
+
+# Ditto for the "secondary_category" here.
+>>> a1.get_secondary_category_list()
+[Life]
+
+# Ditto for the "secondary_category" here.
+>>> a2.get_secondary_category_list()
+[Life]
+
+
+>>> c1.get_primary_article_list()
+[Area man runs]
+>>> c1.get_secondary_article_list()
+[]
+>>> c2.get_primary_article_list()
+[Area man steals, Area man runs]
+>>> c2.get_secondary_article_list()
+[]
+>>> c3.get_primary_article_list()
+[Area man steals]
+>>> c3.get_secondary_article_list()
+[]
+>>> c4.get_primary_article_list()
+[]
+>>> c4.get_secondary_article_list()
+[Area man steals, Area man runs]
+"""