Ticket #14694: 14694-defer-reverse-relations-r17008.2.diff

File 14694-defer-reverse-relations-r17008.2.diff, 4.0 KB (added by Tai Lee, 13 years ago)
  • tests/regressiontests/defer_regress/tests.py

     
    99from django.test import TestCase
    1010
    1111from .models import (ResolveThis, Item, RelatedItem, Child, Leaf, Proxy,
    12     SimpleItem, Feature)
     12    SimpleItem, Feature, OneToOneItem)
    1313
    1414
    1515class DeferRegressionTest(TestCase):
     
    110110                Feature,
    111111                Item,
    112112                Leaf,
     113                OneToOneItem,
    113114                Proxy,
    114115                RelatedItem,
    115116                ResolveThis,
     
    141142                "Leaf_Deferred_name_value",
    142143                "Leaf_Deferred_second_child_value",
    143144                "Leaf_Deferred_value",
     145                "OneToOneItem",
    144146                "Proxy",
    145147                "RelatedItem",
    146148                "RelatedItem_Deferred_",
     
    174176        qs = ResolveThis.objects.defer('num')
    175177        self.assertEqual(1, qs.count())
    176178        self.assertEqual('Foobar', qs[0].name)
     179
     180    def test_reverse_one_to_one_relations(self):
     181        item = Item.objects.create(name="first", value=42)
     182        OneToOneItem.objects.create(item=item, name="second")
     183        self.assertEqual(len(Item.objects.all()), 1)
     184        self.assertEqual(len(Item.objects.defer('one_to_one_item__name')), 1)
  • tests/regressiontests/defer_regress/models.py

     
    4747
    4848class Feature(models.Model):
    4949    item = models.ForeignKey(SimpleItem)
     50
     51class OneToOneItem(models.Model):
     52    item = models.OneToOneField(Item, related_name="one_to_one_item")
     53    name = models.CharField(max_length=15)
  • django/db/models/sql/query.py

     
    1616from django.db.models import signals
    1717from django.db.models.fields import FieldDoesNotExist
    1818from django.db.models.query_utils import InvalidQuery
     19from django.db.models.related import RelatedObject
    1920from django.db.models.sql import aggregates as base_aggregates_module
    2021from django.db.models.sql.constants import *
    2122from django.db.models.sql.datastructures import EmptyResultSet, Empty, MultiJoin
     
    580581            for name in parts[:-1]:
    581582                old_model = cur_model
    582583                source = opts.get_field_by_name(name)[0]
    583                 cur_model = opts.get_field_by_name(name)[0].rel.to
     584                if isinstance(source, RelatedObject):
     585                    cur_model = source.model
     586                else:
     587                    cur_model = source.rel.to
    584588                opts = cur_model._meta
    585589                # Even if we're "just passing through" this model, we must add
    586590                # both the current model's pk and the related reference field
    587591                # to the things we select.
    588                 must_include[old_model].add(source)
     592                # (if it's not a reverse relation) to the things we select.
     593                if not isinstance(source, RelatedObject):
     594                    must_include[old_model].add(source)
    589595                add_to_dict(must_include, cur_model, opts.pk)
    590596            field, model, _, _ = opts.get_field_by_name(parts[-1])
    591597            if model is None:
     
    630636            for model, values in seen.iteritems():
    631637                callback(target, model, values)
    632638
    633 
    634639    def deferred_to_columns_cb(self, target, model, fields):
    635640        """
    636641        Callback used by deferred_to_columns(). The "target" parameter should
     
    642647        for field in fields:
    643648            target[table].add(field.column)
    644649
    645 
    646650    def table_alias(self, table_name, create=False):
    647651        """
    648652        Returns a table alias for the given table_name and whether this is a
Back to Top