Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7530, #7716 -- When using select_related() and encountering a …

…NULL

related object, populate the attribute correctly. Patch from Bastien Kleineidam.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8098 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f48855178daf8f924338f8df322353797ce30081 1 parent aee55ce
Malcolm Tredinnick authored July 27, 2008
6  django/db/models/query.py
@@ -785,7 +785,11 @@ def get_cached_row(klass, row, index_start, max_depth=0, cur_depth=0,
785 785
 
786 786
     restricted = requested is not None
787 787
     index_end = index_start + len(klass._meta.fields)
788  
-    obj = klass(*row[index_start:index_end])
  788
+    fields = row[index_start:index_end]
  789
+    if not [x for x in fields if x is not None]:
  790
+        # If we only have a list of Nones, there was not related object.
  791
+        return None, index_end
  792
+    obj = klass(*fields)
789 793
     for f in klass._meta.fields:
790 794
         if not select_related_descend(f, restricted, requested):
791 795
             continue
18  tests/regressiontests/null_fk/models.py
... ...
@@ -1,8 +1,5 @@
1 1
 """
2  
-Regression tests for proper working of ForeignKey(null=True). Tests these bugs:
3  
-
4  
-    * #7369: FK non-null after null relationship on select_related() generates an invalid query
5  
-
  2
+Regression tests for proper working of ForeignKey(null=True).
6 3
 """
7 4
 
8 5
 from django.db import models
@@ -38,7 +35,8 @@ def __unicode__(self):
38 35
 
39 36
 # Starting from comment, make sure that a .select_related(...) with a specified
40 37
 # set of fields will properly LEFT JOIN multiple levels of NULLs (and the things
41  
-# that come after the NULLs, or else data that should exist won't).
  38
+# that come after the NULLs, or else data that should exist won't). Regression
  39
+# test for #7369.
42 40
 >>> c = Comment.objects.select_related().get(id=1)
43 41
 >>> c.post
44 42
 <Post: First Post>
@@ -47,9 +45,11 @@ def __unicode__(self):
47 45
 None
48 46
 
49 47
 >>> comments = Comment.objects.select_related('post__forum__system_info').all()
50  
->>> [(c.id, c.post.id) for c in comments]
51  
-[(1, 1), (2, None)]
52  
->>> [(c.comment_text, c.post.title) for c in comments]
53  
-[(u'My first comment', u'First Post'), (u'My second comment', None)]
  48
+>>> [(c.id, c.comment_text, c.post) for c in comments]
  49
+[(1, u'My first comment', <Post: First Post>), (2, u'My second comment', None)]
  50
+
  51
+# Regression test for #7530, #7716.
  52
+>>> Comment.objects.select_related('post').filter(post__isnull=True)[0].post is None
  53
+True
54 54
 
55 55
 """}

0 notes on commit f488551

Please sign in to comment.
Something went wrong with that request. Please try again.