diff --git a/django/db/models/query.py b/django/db/models/query.py
index 7492039..f301753 100644
a
|
b
|
def get_cached_row(klass, row, index_start, max_depth=0, cur_depth=0,
|
1203 | 1203 | # If the base object exists, populate the |
1204 | 1204 | # descriptor cache |
1205 | 1205 | setattr(obj, f.get_cache_name(), rel_obj) |
1206 | | if f.unique: |
| 1206 | if f.unique and rel_obj is not None: |
1207 | 1207 | # If the field is unique, populate the |
1208 | 1208 | # reverse descriptor cache on the related object |
1209 | 1209 | setattr(rel_obj, f.related.get_cache_name(), obj) |
diff --git a/tests/regressiontests/select_related_onetoone/models.py b/tests/regressiontests/select_related_onetoone/models.py
index 0014278..6b46366 100644
a
|
b
|
class StatDetails(models.Model):
|
44 | 44 | |
45 | 45 | class AdvancedUserStat(UserStat): |
46 | 46 | pass |
| 47 | |
| 48 | |
| 49 | class Image(models.Model): |
| 50 | name = models.CharField(max_length=100) |
| 51 | |
| 52 | |
| 53 | class Product(models.Model): |
| 54 | name = models.CharField(max_length=100) |
| 55 | image = models.OneToOneField(Image, null=True) |
diff --git a/tests/regressiontests/select_related_onetoone/tests.py b/tests/regressiontests/select_related_onetoone/tests.py
index b9e5beb..5a4a3e4 100644
a
|
b
|
from django import db
|
2 | 2 | from django.conf import settings |
3 | 3 | from django.test import TestCase |
4 | 4 | |
5 | | from models import User, UserProfile, UserStat, UserStatResult, StatDetails, AdvancedUserStat |
| 5 | from models import (User, UserProfile, UserStat, UserStatResult, StatDetails, |
| 6 | AdvancedUserStat, Image, Product) |
6 | 7 | |
7 | 8 | class ReverseSelectRelatedTestCase(TestCase): |
8 | 9 | def setUp(self): |
… |
… |
class ReverseSelectRelatedTestCase(TestCase):
|
81 | 82 | stat = UserStat.objects.select_related('advanceduserstat').get(posts=200) |
82 | 83 | self.assertEqual(stat.advanceduserstat.posts, 200) |
83 | 84 | self.assertQueries(1) |
| 85 | |
| 86 | def test_nullable_relation(self): |
| 87 | im = Image.objects.create(name="imag1") |
| 88 | p1 = Product.objects.create(name="Django Plushie", image=im) |
| 89 | p2 = Product.objects.create(name="Talking Django Plushie") |
| 90 | |
| 91 | self.assertEqual(len(Product.objects.select_related("image")), 2) |