From 5326cd293ef39408c325d592afdb87cb432007bd Mon Sep 17 00:00:00 2001
From: Malcolm Tredinnick <malcolm.tredinnick@gmail.com>
Date: Sun, 29 Jun 2008 09:40:17 +0000
Subject: [PATCH] Factored out a semi-complex if-test that was used in two
 places.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7782 bcc190cf-cafb-0310-a4f2-bffc1f526a37
---
 django/db/models/query.py       |  5 ++---
 django/db/models/query_utils.py | 17 +++++++++++++++++
 django/db/models/sql/query.py   |  6 +++---
 3 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/django/db/models/query.py b/django/db/models/query.py
index 98caaf004c..e92f6c4275 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -3,7 +3,7 @@ import warnings
 from django.conf import settings
 from django.db import connection, transaction, IntegrityError
 from django.db.models.fields import DateField, FieldDoesNotExist
-from django.db.models.query_utils import Q
+from django.db.models.query_utils import Q, select_related_descend
 from django.db.models import signals, sql
 from django.dispatch import dispatcher
 from django.utils.datastructures import SortedDict
@@ -761,8 +761,7 @@ def get_cached_row(klass, row, index_start, max_depth=0, cur_depth=0,
     index_end = index_start + len(klass._meta.fields)
     obj = klass(*row[index_start:index_end])
     for f in klass._meta.fields:
-        if (not f.rel or (not restricted and f.null) or
-                (restricted and f.name not in requested) or f.rel.parent_link):
+        if not select_related_descend(f, restricted, requested):
             continue
         if restricted:
             next = requested[f.name]
diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py
index 0ce7900c74..8dbb1ec667 100644
--- a/django/db/models/query_utils.py
+++ b/django/db/models/query_utils.py
@@ -48,3 +48,20 @@ class Q(tree.Node):
         obj.negate()
         return obj
 
+def select_related_descend(field, restricted, requested):
+    """
+    Returns True if this field should be used to descend deeper for
+    select_related() purposes. Used by both the query construction code
+    (sql.query.fill_related_selections()) and the model instance creation code
+    (query.get_cached_row()).
+    """
+    if not field.rel:
+        return False
+    if field.rel.parent_link:
+        return False
+    if restricted and field.name not in requested:
+        return False
+    if not restricted and field.null:
+        return False
+    return True
+
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index b30133c0e5..ac8222b7b0 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -15,9 +15,10 @@ from django.utils.datastructures import SortedDict
 from django.dispatch import dispatcher
 from django.db import connection
 from django.db.models import signals
+from django.db.models.fields import FieldDoesNotExist
+from django.db.models.query_utils import select_related_descend
 from django.db.models.sql.where import WhereNode, EverythingNode, AND, OR
 from django.db.models.sql.datastructures import Count
-from django.db.models.fields import FieldDoesNotExist
 from django.core.exceptions import FieldError
 from datastructures import EmptyResultSet, Empty, MultiJoin
 from constants import *
@@ -915,8 +916,7 @@ class Query(object):
                 restricted = False
 
         for f, model in opts.get_fields_with_model():
-            if (not f.rel or (restricted and f.name not in requested) or
-                    (not restricted and f.null) or f.rel.parent_link):
+            if not select_related_descend(f, restricted, requested):
                 continue
             dupe_set = orig_dupe_set.copy()
             used = orig_used.copy()