Author: lukeplant
Date: 2006-04-05 06:46:30 -0500 (Wed, 05 Apr 2006)
New Revision: 2612

Added:
   django/branches/magic-removal/tests/modeltests/m2m_and_m2o/
   django/branches/magic-removal/tests/modeltests/m2m_and_m2o/__init__.py
   django/branches/magic-removal/tests/modeltests/m2m_and_m2o/models.py
Log:
magic-removal: Added tests for bug #1535 (currently failing)


Added: django/branches/magic-removal/tests/modeltests/m2m_and_m2o/__init__.py
===================================================================

Added: django/branches/magic-removal/tests/modeltests/m2m_and_m2o/models.py
===================================================================
--- django/branches/magic-removal/tests/modeltests/m2m_and_m2o/models.py        
2006-04-05 07:35:47 UTC (rev 2611)
+++ django/branches/magic-removal/tests/modeltests/m2m_and_m2o/models.py        
2006-04-05 11:46:30 UTC (rev 2612)
@@ -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>]
+"""


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates
-~----------~----~----~----~------~----~------~--~---

Reply via email to