Ticket #13839: fix13839.patch

File fix13839.patch, 2.8 KB (added by Matthias Kestenholz, 14 years ago)
  • django/db/models/query.py

    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,  
    12591259                if obj is not None:
    12601260                    # If the field is unique, populate the
    12611261                    # 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)
    12631264                if rel_obj is not None:
    12641265                    # If the related object exists, populate
    12651266                    # the descriptor cache.
  • tests/regressiontests/select_related_onetoone/models.py

    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):  
    5252class Product(models.Model):
    5353    name = models.CharField(max_length=100)
    5454    image = models.OneToOneField(Image, null=True)
     55
     56
     57class UserChild(User):
     58    pass
  • tests/regressiontests/select_related_onetoone/tests.py

    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  
    33from django.test import TestCase
    44
    55from models import (User, UserProfile, UserStat, UserStatResult, StatDetails,
    6     AdvancedUserStat, Image, Product)
     6    AdvancedUserStat, Image, Product, UserChild)
    77
    88class ReverseSelectRelatedTestCase(TestCase):
    99    def setUp(self):
    class ReverseSelectRelatedTestCase(TestCase):  
    9292        p2 = Product.objects.create(name="Talking Django Plushie")
    9393
    9494        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)
Back to Top