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):
|
498 | 498 | db = self._db or router.db_for_read(self.model, instance=self.instance) |
499 | 499 | qs = super(RelatedManager, self).get_query_set().using(db).filter(**self.core_filters) |
500 | 500 | 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=[]) |
502 | 503 | qs._known_related_objects = {rel_field: {self.instance.pk: self.instance}} |
503 | 504 | return qs |
504 | 505 | |
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):
|
63 | 63 | |
64 | 64 | def __str__(self): |
65 | 65 | return self.name |
| 66 | |
| 67 | |
| 68 | # Bug #19652 |
| 69 | class ObjectQuerySet(models.query.QuerySet): |
| 70 | pass |
| 71 | |
| 72 | class 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 | |
| 79 | class RelatedObject(models.Model): |
| 80 | pass |
| 81 | |
| 82 | |
| 83 | class Object(models.Model): |
| 84 | related = models.ForeignKey(RelatedObject, related_name='objs') |
| 85 | |
| 86 | objects = ObjectManager() |
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
|
3 | 3 | from django.test import TestCase |
4 | 4 | from django.utils import six |
5 | 5 | |
6 | | from .models import Person, Book, Car, PersonManager, PublishedBookManager |
| 6 | from .models import ObjectQuerySet, RelatedObject, Person, Book, Car, PersonManager, PublishedBookManager |
7 | 7 | |
8 | 8 | |
9 | 9 | class CustomManagerTests(TestCase): |
… |
… |
class CustomManagerTests(TestCase):
|
72 | 72 | ], |
73 | 73 | lambda c: c.name |
74 | 74 | ) |
| 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) |