Ticket #17886: louter2.diff

File louter2.diff, 4.1 KB (added by milosu, 12 years ago)

polished patch

  • django/db/models/sql/query.py

    diff django/db/models/sql/query.py django/db/models/sql/query.py
     
    913913            # Not all tables need to be joined to anything. No join type
    914914            # means the later columns are ignored.
    915915            join_type = None
    916         elif promote or outer_if_first:
     916        elif promote or outer_if_first or \
     917          self.alias_map[connection[0]][JOIN_TYPE] == self.LOUTER:
     918            # if preceeding alias (connection[0]) has LEFT OUTER JOIN
     919            # we should promote it
    917920            join_type = self.LOUTER
    918921        else:
    919922            join_type = self.INNER
  • tests/regressiontests/queries/models.py

    diff tests/regressiontests/queries/models.py tests/regressiontests/queries/models.py
     
    346346
    347347    def __unicode__(self):
    348348        return "one2one " + self.new_name
     349
     350class DefinitionOfDoubleCategorizedObject(models.Model):
     351    name = models.CharField(max_length=50)
     352
     353class CategorizedObject(models.Model):
     354    name = models.CharField(max_length=50)
     355    category = models.ForeignKey(SimpleCategory)
     356
     357class DoubleCategorizedObject(models.Model):
     358    name = models.CharField(max_length=50)
     359    rel_a = models.ForeignKey(CategorizedObject, null=True, blank=True, related_name="rel_a_double")
     360    rel_b = models.ManyToManyField(CategorizedObject, null=True, blank=True, related_name="rel_b_double")
     361    definition = models.ForeignKey(DefinitionOfDoubleCategorizedObject)
  • tests/regressiontests/queries/tests.py

    diff tests/regressiontests/queries/tests.py tests/regressiontests/queries/tests.py
     
    33import datetime
    44import pickle
    55import sys
     6import operator
    67
    78from django.conf import settings
    89from django.core.exceptions import FieldError
     
    1819    ManagedModel, Member, NamedCategory, Note, Number, Plaything, PointerA,
    1920    Ranking, Related, Report, ReservedName, Tag, TvChef, Valid, X, Food, Eaten,
    2021    Node, ObjectA, ObjectB, ObjectC, CategoryItem, SimpleCategory,
    21     SpecialCategory, OneToOneCategory)
     22    SpecialCategory, OneToOneCategory, DoubleCategorizedObject, CategorizedObject,
     23    DefinitionOfDoubleCategorizedObject)
    2224
    2325
    2426class BaseQuerysetTest(TestCase):
     
    18941896            DumbCategory.objects.create()
    18951897        except TypeError:
    18961898            self.fail("Creation of an instance of a model with only the PK field shouldn't error out after bulk insert refactoring (#17056)")
     1899
     1900class Queries6Test(BaseQuerysetTest):
     1901    def setUp(self):
     1902        c1 = SimpleCategory.objects.create(name="category1")
     1903        c2 = SimpleCategory.objects.create(name="category2")
     1904        cat_obj1 = CategorizedObject.objects.create(category=c1, name='cat1')
     1905        cat_obj2 = CategorizedObject.objects.create(category=c2, name='cat2')
     1906        definition = DefinitionOfDoubleCategorizedObject()
     1907        definition.name = 'test def'
     1908        definition.save()
     1909        double_cat_obj = DoubleCategorizedObject()
     1910        double_cat_obj.name = "test obj"
     1911        double_cat_obj.rel_a = cat_obj1
     1912        double_cat_obj.definition = definition
     1913        double_cat_obj.save()
     1914
     1915    def test_or_query(self):
     1916        search_query = 'test def'
     1917        or_queries = [
     1918                Q(name__contains= search_query),
     1919                Q(doublecategorizedobject__rel_a__name__icontains= search_query),
     1920                Q(doublecategorizedobject__rel_a__category__name__icontains = search_query),
     1921                Q(doublecategorizedobject__rel_b__name__icontains= search_query),
     1922                Q(doublecategorizedobject__rel_b__category__name__icontains = search_query),
     1923        ]
     1924        q_obj = reduce(operator.or_, or_queries)
     1925        qset = DefinitionOfDoubleCategorizedObject.objects.filter(q_obj).select_related()
     1926        self.assertTrue(len(qset) == 1)
     1927        self.assertNotIn('INNER JOIN', str(qset.query))
Back to Top