Ticket #12240: django-select-related-join-chain.diff

File django-select-related-join-chain.diff, 2.5 KB (added by Alex Gaynor, 15 years ago)
  • django/db/models/sql/compiler.py

    diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
    index f364b1d..b7d63d3 100644
    a b class SQLCompiler(object):  
    530530            avoid = avoid_set.copy()
    531531            dupe_set = orig_dupe_set.copy()
    532532            table = f.rel.to._meta.db_table
    533             if nullable or f.null:
    534                 promote = True
    535             else:
    536                 promote = False
     533            promote = nullable or f.null
    537534            if model:
    538535                int_opts = opts
    539536                alias = root_alias
    class SQLCompiler(object):  
    584581                next = requested.get(f.name, {})
    585582            else:
    586583                next = False
    587             if f.null is not None:
    588                 new_nullable = f.null
    589             else:
    590                 new_nullable = None
     584            new_nullable = f.null or promote
    591585            for dupe_opts, dupe_col in dupe_set:
    592586                self.query.update_dupe_avoidance(dupe_opts, dupe_col, alias)
    593587            self.fill_related_selections(f.rel.to._meta, alias, cur_depth + 1,
  • tests/regressiontests/null_fk/models.py

    diff --git a/tests/regressiontests/null_fk/models.py b/tests/regressiontests/null_fk/models.py
    index 4988781..1f191da 100644
    a b Regression tests for proper working of ForeignKey(null=True).  
    44
    55from django.db import models
    66
     7class SystemDetails(models.Model):
     8    details = models.TextField()
     9
    710class SystemInfo(models.Model):
     11    system_details = models.ForeignKey(SystemDetails)
    812    system_name = models.CharField(max_length=32)
    913
    1014class Forum(models.Model):
    class Comment(models.Model):  
    3034
    3135__test__ = {'API_TESTS':"""
    3236
    33 >>> s = SystemInfo.objects.create(system_name='First forum')
     37>>> d = SystemDetails.objects.create(details='First details')
     38>>> s = SystemInfo.objects.create(system_name='First forum', system_details=d)
    3439>>> f = Forum.objects.create(system_info=s, forum_name='First forum')
    3540>>> p = Post.objects.create(forum=f, title='First Post')
    3641>>> c1 = Comment.objects.create(post=p, comment_text='My first comment')
    None  
    5560>>> Comment.objects.select_related('post').filter(post__isnull=True)[0].post is None
    5661True
    5762
     63>>> comments = Comment.objects.select_related('post__forum__system_info__system_details')
     64>>> [(c.id, c.comment_text, c.post) for c in comments]
     65[(1, u'My first comment', <Post: First Post>), (2, u'My second comment', None)]
     66
    5867"""}
Back to Top