Ticket #12819: django-select-related.diff

File django-select-related.diff, 2.4 KB (added by Alex Gaynor, 15 years ago)
  • django/db/models/query.py

    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,  
    12031203                # If the base object exists, populate the
    12041204                # descriptor cache
    12051205                setattr(obj, f.get_cache_name(), rel_obj)
    1206             if f.unique:
     1206            if f.unique and rel_obj is not None:
    12071207                # If the field is unique, populate the
    12081208                # reverse descriptor cache on the related object
    12091209                setattr(rel_obj, f.related.get_cache_name(), obj)
  • 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 0014278..6b46366 100644
    a b class StatDetails(models.Model):  
    4444
    4545class AdvancedUserStat(UserStat):
    4646    pass
     47
     48
     49class Image(models.Model):
     50    name = models.CharField(max_length=100)
     51
     52
     53class Product(models.Model):
     54    name = models.CharField(max_length=100)
     55    image = models.OneToOneField(Image, null=True)
  • 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 b9e5beb..5a4a3e4 100644
    a b from django import db  
    22from django.conf import settings
    33from django.test import TestCase
    44
    5 from models import User, UserProfile, UserStat, UserStatResult, StatDetails, AdvancedUserStat
     5from models import (User, UserProfile, UserStat, UserStatResult, StatDetails,
     6    AdvancedUserStat, Image, Product)
    67
    78class ReverseSelectRelatedTestCase(TestCase):
    89    def setUp(self):
    class ReverseSelectRelatedTestCase(TestCase):  
    8182        stat = UserStat.objects.select_related('advanceduserstat').get(posts=200)
    8283        self.assertEqual(stat.advanceduserstat.posts, 200)
    8384        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)
Back to Top