Code

Ticket #17485: defer_select_related.patch

File defer_select_related.patch, 2.2 KB (added by koniiiik, 2 years ago)

Fix for the first bug along with tests for it.

  • django/db/models/query.py

    From d6bd576ee0ba72ea69b4c74d13223e5c61f63dd3 Mon Sep 17 00:00:00 2001
    From: Michal Petrucha <michal.petrucha@ksp.sk>
    Date: Sat, 31 Dec 2011 23:23:21 +0100
    Subject: [PATCH] Fixed defer for ForeignKeys with select_related.
    
    
    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index fd531dd..0f3c39a 100644
    a b def get_klass_info(klass, max_depth=0, cur_depth=0, requested=None, 
    12811281        # Build the list of fields that *haven't* been requested 
    12821282        for field, model in klass._meta.get_fields_with_model(): 
    12831283            if field.name not in load_fields: 
    1284                 skip.add(field.name) 
     1284                skip.add(field.attname) 
    12851285            elif local_only and model is not None: 
    12861286                continue 
    12871287            else: 
  • tests/regressiontests/defer_regress/tests.py

    diff --git a/tests/regressiontests/defer_regress/tests.py b/tests/regressiontests/defer_regress/tests.py
    index 4afe39b..c1a8a68 100644
    a b class DeferRegressionTest(TestCase): 
    139139                "Leaf", 
    140140                "Leaf_Deferred_child_id_second_child_id_value", 
    141141                "Leaf_Deferred_name_value", 
    142                 "Leaf_Deferred_second_child_value", 
     142                "Leaf_Deferred_second_child_id_value", 
    143143                "Leaf_Deferred_value", 
    144144                "Proxy", 
    145145                "RelatedItem", 
    class DeferRegressionTest(TestCase): 
    174174        qs = ResolveThis.objects.defer('num') 
    175175        self.assertEqual(1, qs.count()) 
    176176        self.assertEqual('Foobar', qs[0].name) 
     177 
     178    def test_defer_with_select_related(self): 
     179        item1 = Item.objects.create(name="first", value=47) 
     180        item2 = Item.objects.create(name="second", value=42) 
     181        related = RelatedItem.objects.create(item=item1) 
     182 
     183        obj = RelatedItem.objects.defer('item').select_related('item').get() 
     184        self.assertEqual(obj.item, item1) 
     185        self.assertEqual(obj.item_id, item1.id) 
     186 
     187        obj.item = item2 
     188        obj.save() 
     189 
     190        obj = RelatedItem.objects.defer('item').select_related('item').get() 
     191        self.assertEqual(obj.item, item2) 
     192        self.assertEqual(obj.item_id, item2.id)