diff --git a/django/db/models/query.py b/django/db/models/query.py
index e16fca1..2cef5e5 100644
a
|
b
|
def get_cached_row(klass, row, index_start, using, max_depth=0, cur_depth=0,
|
1259 | 1259 | if obj is not None: |
1260 | 1260 | # If the field is unique, populate the |
1261 | 1261 | # reverse descriptor cache |
1262 | | setattr(obj, f.related.get_cache_name(), rel_obj) |
| 1262 | if rel_obj is not None: |
| 1263 | setattr(obj, f.related.get_cache_name(), rel_obj) |
1263 | 1264 | if rel_obj is not None: |
1264 | 1265 | # If the related object exists, populate |
1265 | 1266 | # the descriptor cache. |
diff --git a/tests/regressiontests/select_related_onetoone/models.py b/tests/regressiontests/select_related_onetoone/models.py
index 3d6da9b..1be9eb8 100644
a
|
b
|
class Image(models.Model):
|
52 | 52 | class Product(models.Model): |
53 | 53 | name = models.CharField(max_length=100) |
54 | 54 | image = models.OneToOneField(Image, null=True) |
| 55 | |
| 56 | |
| 57 | class UserChild(User): |
| 58 | pass |
diff --git a/tests/regressiontests/select_related_onetoone/tests.py b/tests/regressiontests/select_related_onetoone/tests.py
index 4ccb584..e8c1541 100644
a
|
b
|
from django.conf import settings
|
3 | 3 | from django.test import TestCase |
4 | 4 | |
5 | 5 | from models import (User, UserProfile, UserStat, UserStatResult, StatDetails, |
6 | | AdvancedUserStat, Image, Product) |
| 6 | AdvancedUserStat, Image, Product, UserChild) |
7 | 7 | |
8 | 8 | class ReverseSelectRelatedTestCase(TestCase): |
9 | 9 | def setUp(self): |
… |
… |
class ReverseSelectRelatedTestCase(TestCase):
|
92 | 92 | p2 = Product.objects.create(name="Talking Django Plushie") |
93 | 93 | |
94 | 94 | self.assertEqual(len(Product.objects.select_related("image")), 2) |
| 95 | |
| 96 | def test_select_related_with_inheritance(self): |
| 97 | """ |
| 98 | select_related should not pre-fill the SingleRelatedObjectDescriptor |
| 99 | cache with None when the related object does not exist. |
| 100 | |
| 101 | This tests both the inheritance and the standard OneToOneField usage |
| 102 | case. |
| 103 | """ |
| 104 | User.objects.create(username='test13839') |
| 105 | user = User.objects.filter(username='test13839')[0] |
| 106 | |
| 107 | self.assertRaises(UserChild.DoesNotExist, lambda: user.userchild) |
| 108 | self.assertRaises(UserProfile.DoesNotExist, lambda: user.userprofile) |
| 109 | |
| 110 | user = User.objects.filter(username='test13839').select_related('userchild', 'userprofile')[0] |
| 111 | self.assertRaises(UserChild.DoesNotExist, lambda: user.userchild) |
| 112 | self.assertRaises(UserProfile.DoesNotExist, lambda: user.userprofile) |