From e2296e7f0acf9c50868ab997ba565c1f47beb45b Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sat, 28 May 2016 09:14:02 -0400 Subject: Fixed #26667 -- Fixed a regression in queries on a OneToOneField that has to_field and primary_key=True. Thanks Simon Charette for review. --- tests/one_to_one/models.py | 6 +++++- tests/one_to_one/tests.py | 10 ++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) (limited to 'tests/one_to_one') diff --git a/tests/one_to_one/models.py b/tests/one_to_one/models.py index 2742fb186e..0f4aa70f6c 100644 --- a/tests/one_to_one/models.py +++ b/tests/one_to_one/models.py @@ -83,7 +83,7 @@ class MultiModel(models.Model): class Target(models.Model): - name = models.CharField(max_length=50) + name = models.CharField(max_length=50, unique=True) class Pointer(models.Model): @@ -98,6 +98,10 @@ class HiddenPointer(models.Model): target = models.OneToOneField(Target, models.CASCADE, related_name='hidden+') +class ToFieldPointer(models.Model): + target = models.OneToOneField(Target, models.CASCADE, to_field='name', primary_key=True) + + # Test related objects visibility. class SchoolManager(models.Manager): def get_queryset(self): diff --git a/tests/one_to_one/tests.py b/tests/one_to_one/tests.py index 7e8c9c5dc3..2cb6568221 100644 --- a/tests/one_to_one/tests.py +++ b/tests/one_to_one/tests.py @@ -6,8 +6,8 @@ from django.utils.deprecation import RemovedInDjango20Warning from .models import ( Bar, Director, Favorites, HiddenPointer, ManualPrimaryKey, MultiModel, - Place, Pointer, RelatedModel, Restaurant, School, Target, UndergroundBar, - Waiter, + Place, Pointer, RelatedModel, Restaurant, School, Target, ToFieldPointer, + UndergroundBar, Waiter, ) @@ -530,3 +530,9 @@ class OneToOneTests(TestCase): r = Restaurant.objects.first() r2 = Restaurant.objects.filter(pk__exact=r).first() self.assertEqual(r, r2) + + def test_primary_key_to_field_filter(self): + target = Target.objects.create(name='foo') + pointer = ToFieldPointer.objects.create(target=target) + self.assertQuerysetEqual(ToFieldPointer.objects.filter(target=target), [pointer], lambda x: x) + self.assertQuerysetEqual(ToFieldPointer.objects.filter(pk__exact=pointer), [pointer], lambda x: x) -- cgit v1.2.1