diff -r d0dc9aaff6af django/db/models/sql/query.py
--- a/django/db/models/sql/query.py	Sat Dec 12 22:04:04 2009 +0000
+++ b/django/db/models/sql/query.py	Sat Dec 12 20:00:58 2009 -0500
@@ -1670,13 +1670,15 @@
             for child in q_object.children:
                 if connector == OR:
                     refcounts_before = self.alias_refcount.copy()
+                # a subtree may also be necesary for add_filter, in the
+                # case where extra_filters is a non-empty list
+                self.where.start_subtree(connector)
                 if isinstance(child, Node):
-                    self.where.start_subtree(connector)
                     self.add_q(child, used_aliases)
-                    self.where.end_subtree()
                 else:
                     self.add_filter(child, connector, q_object.negated,
                             can_reuse=used_aliases)
+                self.where.end_subtree()
                 if connector == OR:
                     # Aliases that were newly added or not used at all need to
                     # be promoted to outer joins if they are nullable relations.
diff -r d0dc9aaff6af tests/regressiontests/generic_relations_regress/models.py
--- a/tests/regressiontests/generic_relations_regress/models.py	Sat Dec 12 22:04:04 2009 +0000
+++ b/tests/regressiontests/generic_relations_regress/models.py	Sat Dec 12 20:00:58 2009 -0500
@@ -2,7 +2,11 @@
 from django.contrib.contenttypes import generic
 from django.contrib.contenttypes.models import ContentType
 
+__all__ = ('Link', 'Place', 'Restaurant', 'Contact', 'Organization', 'Note')
+
+
 class Link(models.Model):
+
     content_type = models.ForeignKey(ContentType)
     object_id = models.PositiveIntegerField()
     content_object = generic.GenericForeignKey()
@@ -10,13 +14,37 @@
     def __unicode__(self):
         return "Link to %s id=%s" % (self.content_type, self.object_id)
 
+
 class Place(models.Model):
+
     name = models.CharField(max_length=100)
     links = generic.GenericRelation(Link)
-    
+
     def __unicode__(self):
         return "Place: %s" % self.name
-    
-class Restaurant(Place): 
+
+
+class Restaurant(Place):
+
     def __unicode__(self):
-        return "Restaurant: %s" % self.name
\ No newline at end of file
+        return "Restaurant: %s" % self.name
+
+
+# models for test_q_object_or:
+class Note(models.Model):
+
+    content_type = models.ForeignKey(ContentType)
+    object_id = models.PositiveIntegerField()
+    content_object = generic.GenericForeignKey()
+    note = models.TextField()
+
+
+class Contact(models.Model):
+
+    notes = generic.GenericRelation(Note)
+
+
+class Organization(models.Model):
+
+    name = models.CharField(max_length=255)
+    contacts = models.ManyToManyField(Contact, related_name='organizations')
diff -r d0dc9aaff6af tests/regressiontests/generic_relations_regress/tests.py
--- a/tests/regressiontests/generic_relations_regress/tests.py	Sat Dec 12 22:04:04 2009 +0000
+++ b/tests/regressiontests/generic_relations_regress/tests.py	Sat Dec 12 20:00:58 2009 -0500
@@ -1,19 +1,48 @@
 from django.test import TestCase
 from django.contrib.contenttypes.models import ContentType
-from models import Link, Place, Restaurant
+from models import *
+from django.db.models import Q
+
 
 class GenericRelationTests(TestCase):
-    
+
     def test_inherited_models_content_type(self):
         """
         Test that GenericRelations on inherited classes use the correct content
         type.
         """
-        
+
         p = Place.objects.create(name="South Park")
-        r = Restaurant.objects.create(name="Chubby's")        
+        r = Restaurant.objects.create(name="Chubby's")
         l1 = Link.objects.create(content_object=p)
         l2 = Link.objects.create(content_object=r)
         self.assertEqual(list(p.links.all()), [l1])
         self.assertEqual(list(r.links.all()), [l2])
-        
\ No newline at end of file
+
+    def test_q_object_or(self):
+        """
+        Tests that SQL query parameters for generic relations are properly
+        grouped when OR is used.
+
+        Test for bug http://code.djangoproject.com/ticket/11535
+
+        In this bug the first query (below) works while the second, with the
+        query parameters the same but in reverse order, does not.
+
+        The issue is that the generic relation conditions do not get properly
+        grouped in parentheses.
+        """
+        note_contact = Contact.objects.create()
+        org_contact = Contact.objects.create()
+        note = Note.objects.create(note='note', content_object=note_contact)
+        org = Organization.objects.create(name='org name')
+        org.contacts.add(org_contact)
+        # search with a non-matching note and a matching org name
+        qs = Contact.objects.filter(Q(notes__note__icontains=r'other note') |
+                            Q(organizations__name__icontains=r'org name'))
+        self.assertTrue(org_contact in qs)
+        # search again, with the same query parameters, in reverse order
+        qs = Contact.objects.filter(
+            Q(organizations__name__icontains=r'org name') |
+            Q(notes__note__icontains=r'other note'))
+        self.assertTrue(org_contact in qs)
