Ticket #19652: 0001-Fixed-19652-Fixed-a-regression-introduced-by-9d6d0de.patch

File 0001-Fixed-19652-Fixed-a-regression-introduced-by-9d6d0de.patch, 3.0 KB (added by Simon Charette, 12 years ago)
  • django/db/models/fields/related.py

    From d14ba5485592c482ebc21094039b72ff094cad82 Mon Sep 17 00:00:00 2001
    From: Simon Charette <charette.s@gmail.com>
    Date: Tue, 22 Jan 2013 17:54:12 -0500
    Subject: [PATCH] Fixed #19652 -- Fixed a regression introduced by 9d6d0de7c1.
    
    ---
     django/db/models/fields/related.py         |    3 ++-
     tests/modeltests/custom_managers/models.py |   21 +++++++++++++++++++++
     tests/modeltests/custom_managers/tests.py  |    9 ++++++++-
     3 files changed, 31 insertions(+), 2 deletions(-)
    
    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index 2e390c5..56abedc 100644
    a b class ForeignRelatedObjectsDescriptor(object):  
    498498                    db = self._db or router.db_for_read(self.model, instance=self.instance)
    499499                    qs = super(RelatedManager, self).get_query_set().using(db).filter(**self.core_filters)
    500500                    if getattr(self.instance, attname) is None:
    501                         return qs.none()
     501                        # We can return qs.none() here, see #19652
     502                        return qs.filter(pk__in=[])
    502503                    qs._known_related_objects = {rel_field: {self.instance.pk: self.instance}}
    503504                    return qs
    504505
  • tests/modeltests/custom_managers/models.py

    diff --git a/tests/modeltests/custom_managers/models.py b/tests/modeltests/custom_managers/models.py
    index de7c177..f224ec0 100644
    a b class Car(models.Model):  
    6363
    6464    def __str__(self):
    6565        return self.name
     66
     67
     68# Bug #19652
     69class ObjectQuerySet(models.query.QuerySet):
     70    pass
     71
     72class ObjectManager(models.Manager):
     73    use_for_related_fields = True
     74
     75    def get_query_set(self):
     76        return ObjectQuerySet(self.model, using=self._db)
     77
     78
     79class RelatedObject(models.Model):
     80    pass
     81
     82
     83class Object(models.Model):
     84    related = models.ForeignKey(RelatedObject, related_name='objs')
     85
     86    objects = ObjectManager()
  • tests/modeltests/custom_managers/tests.py

    diff --git a/tests/modeltests/custom_managers/tests.py b/tests/modeltests/custom_managers/tests.py
    index 294920d..086a82a 100644
    a b from __future__ import absolute_import  
    33from django.test import TestCase
    44from django.utils import six
    55
    6 from .models import Person, Book, Car, PersonManager, PublishedBookManager
     6from .models import ObjectQuerySet, RelatedObject, Person, Book, Car, PersonManager, PublishedBookManager
    77
    88
    99class CustomManagerTests(TestCase):
    class CustomManagerTests(TestCase):  
    7272            ],
    7373            lambda c: c.name
    7474        )
     75
     76    def test_related_manager(self):
     77        """
     78        Make sure un-saved objects related managers always return an instance
     79        of the same class the manager's `get_query_set` returns. Refs #19652.
     80        """
     81        self.assertIsInstance(RelatedObject().objs.all(), ObjectQuerySet)
Back to Top