Ticket #10977: 10977.diff

File 10977.diff, 2.9 KB (added by Ramiro Morales, 14 years ago)

Patch with fix by clement and simplified tests

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

    diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
    a b  
    455455            if not rhs.alias_refcount[alias]:
    456456                # An unused alias.
    457457                continue
     458            lhs_alias = rhs.alias_map[alias][LHS_ALIAS]
     459            aux = (change_map.get(lhs_alias, lhs_alias),) + rhs.rev_join_map[alias][1:]
    458460            promote = (rhs.alias_map[alias][JOIN_TYPE] == self.LOUTER)
    459             new_alias = self.join(rhs.rev_join_map[alias],
     461            new_alias = self.join(aux,
    460462                    (conjunction and not first), used, promote, not conjunction)
    461463            used.add(new_alias)
    462464            change_map[alias] = new_alias
  • tests/regressiontests/queries/models.py

    diff --git a/tests/regressiontests/queries/models.py b/tests/regressiontests/queries/models.py
    a b  
    294294
    295295    def __unicode__(self):
    296296        return u"%s" % self.num
     297
     298# Models for #10977
     299
     300# A base class for Sponsor and Team
     301class Brand(models.Model):
     302    pass
     303
     304class Mark(models.Model):
     305    brand = models.ForeignKey(Brand)
     306    name = models.CharField(max_length=20)
     307
     308class Sponsor(Brand):
     309    pass
     310
     311class Team(Brand):
     312    name = models.CharField(max_length=20)
     313    sponsor = models.ForeignKey(Sponsor)
     314
     315    def __unicode__(self):
     316        return self.name
  • tests/regressiontests/queries/tests.py

    diff --git a/tests/regressiontests/queries/tests.py b/tests/regressiontests/queries/tests.py
    a b  
    1414from models import (Annotation, Article, Author, Celebrity, Child, Cover, Detail,
    1515    DumbCategory, ExtraInfo, Fan, Item, LeafA, LoopX, LoopZ, ManagedModel,
    1616    Member, NamedCategory, Note, Number, Plaything, PointerA, Ranking, Related,
    17     Report, ReservedName, Tag, TvChef, Valid, X, Food, Eaten, Node)
     17    Report, ReservedName, Tag, TvChef, Valid, X, Food, Eaten, Node,
     18    Sponsor, Team, Mark)
    1819
    1920
    2021class BaseQuerysetTest(TestCase):
     
    16581659            Number.objects.filter(num__in=numbers).count(),
    16591660            2500
    16601661        )
     1662
     1663
     1664class QuerysetIntersectionTests(TestCase):
     1665    """
     1666    Tests for merging querysets that have been filtered on multiple elements
     1667    from the same base class
     1668    """
     1669
     1670    def test_models_with_same_parent(self):
     1671        s1 = Sponsor.objects.create()
     1672        s2 = Sponsor.objects.create()
     1673        Team.objects.create(sponsor=s1, name='Team A')
     1674        Team.objects.create(sponsor=s2, name='Team B')
     1675        Mark.objects.create(name='Product A', brand=s1)
     1676
     1677        qs1 = Team.objects.all()
     1678
     1679        qs2 = Team.objects.filter(
     1680            Q(sponsor__mark__name='Product A') |
     1681            Q(sponsor__mark__name='Product A')
     1682        )
     1683        r = qs1 & qs2
     1684        self.assertQuerysetEqual(r, ['<Team: Team A>'])
Back to Top