summaryrefslogtreecommitdiff
path: root/tests/modeltests/m2m_and_m2o/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_and_m2o/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_and_m2o/models.py')
-rw-r--r--tests/modeltests/m2m_and_m2o/models.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/modeltests/m2m_and_m2o/models.py b/tests/modeltests/m2m_and_m2o/models.py
new file mode 100644
index 0000000000..29631e5779
--- /dev/null
+++ b/tests/modeltests/m2m_and_m2o/models.py
@@ -0,0 +1,66 @@
+"""
+27. Many-to-many and many-to-one relationships to the same table.
+
+This is a response to bug #1535
+
+"""
+
+from django.db import models
+
+class User(models.Model):
+ username = models.CharField(maxlength=20)
+
+class Issue(models.Model):
+ num = models.IntegerField()
+ cc = models.ManyToManyField(User, blank=True, related_name='test_issue_cc')
+ client = models.ForeignKey(User, related_name='test_issue_client')
+ def __repr__(self):
+ return "<Issue %d>" % (self.num,)
+
+ class Meta:
+ ordering = ('num',)
+
+
+API_TESTS = """
+>>> Issue.objects.all()
+[]
+>>> r = User(username='russell')
+>>> r.save()
+>>> g = User(username='gustav')
+>>> g.save()
+>>> i = Issue(num=1)
+>>> i.client = r
+>>> i.validate()
+{}
+>>> i.save()
+>>> i2 = Issue(num=2)
+>>> i2.client = r
+>>> i2.validate()
+{}
+>>> i2.save()
+>>> i2.cc.add(r)
+>>> i3 = Issue(num=3)
+>>> i3.client = g
+>>> i3.validate()
+{}
+>>> i3.save()
+>>> i3.cc.add(r)
+>>> from django.db.models.query import Q
+>>> Issue.objects.filter(client=r.id)
+[<Issue 1>, <Issue 2>]
+>>> Issue.objects.filter(client=g.id)
+[<Issue 3>]
+>>> Issue.objects.filter(cc__id__exact=g.id)
+[]
+>>> Issue.objects.filter(cc__id__exact=r.id)
+[<Issue 2>, <Issue 3>]
+
+# Queries that combine results from the m2m and the m2o relationship.
+# 3 ways of saying the same thing:
+>>> Issue.objects.filter(Q(cc__id__exact=r.id) | Q(client=r.id))
+[<Issue 1>, <Issue 2>, <Issue 3>]
+>>> Issue.objects.filter(cc__id__exact=r.id) | Issue.objects.filter(client=r.id)
+[<Issue 1>, <Issue 2>, <Issue 3>]
+>>> Issue.objects.filter(Q(client=r.id) | Q(cc__id__exact=r.id))
+[<Issue 1>, <Issue 2>, <Issue 3>]
+"""