Fixed #12819. Fixed a bug with caching values of nullable 1to1 fields. Thanks, s.angel@twidi.com for the initial patch, and Alex Gaynor for the tests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12543 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Joseph Kocherhans 2010-02-23 19:24:27 +00:00
parent 3f443363f9
commit 65b451ae3e
3 changed files with 19 additions and 2 deletions

View File

@ -1203,7 +1203,7 @@ def get_cached_row(klass, row, index_start, max_depth=0, cur_depth=0,
# If the base object exists, populate the
# descriptor cache
setattr(obj, f.get_cache_name(), rel_obj)
if f.unique:
if f.unique and rel_obj is not None:
# If the field is unique, populate the
# reverse descriptor cache on the related object
setattr(rel_obj, f.related.get_cache_name(), obj)

View File

@ -44,3 +44,12 @@ class StatDetails(models.Model):
class AdvancedUserStat(UserStat):
pass
class Image(models.Model):
name = models.CharField(max_length=100)
class Product(models.Model):
name = models.CharField(max_length=100)
image = models.OneToOneField(Image, null=True)

View File

@ -2,7 +2,8 @@ from django import db
from django.conf import settings
from django.test import TestCase
from models import User, UserProfile, UserStat, UserStatResult, StatDetails, AdvancedUserStat
from models import (User, UserProfile, UserStat, UserStatResult, StatDetails,
AdvancedUserStat, Image, Product)
class ReverseSelectRelatedTestCase(TestCase):
def setUp(self):
@ -81,3 +82,10 @@ class ReverseSelectRelatedTestCase(TestCase):
stat = UserStat.objects.select_related('advanceduserstat').get(posts=200)
self.assertEqual(stat.advanceduserstat.posts, 200)
self.assertQueries(1)
def test_nullable_relation(self):
im = Image.objects.create(name="imag1")
p1 = Product.objects.create(name="Django Plushie", image=im)
p2 = Product.objects.create(name="Talking Django Plushie")
self.assertEqual(len(Product.objects.select_related("image")), 2)