diff options
author | Paulo <commonzenpython@gmail.com> | 2017-08-14 17:27:25 -0400 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2017-08-21 16:14:13 -0400 |
commit | fea9cb46aacc73cabac883a806ccb7fdc1f979dd (patch) | |
tree | f4d58795bc081a7b8a36df4f2edf8478f77ed024 /tests/prefetch_related | |
parent | b5ad5c628a0327c2208d76e5cacb3cb6f48750b5 (diff) | |
download | django-fea9cb46aacc73cabac883a806ccb7fdc1f979dd.tar.gz |
Fixed #28375 -- Fixed KeyError crash on reverse prefetch of a model with OneToOneField primary key to a non-pk field.
Diffstat (limited to 'tests/prefetch_related')
-rw-r--r-- | tests/prefetch_related/models.py | 7 | ||||
-rw-r--r-- | tests/prefetch_related/tests.py | 17 |
2 files changed, 23 insertions, 1 deletions
diff --git a/tests/prefetch_related/models.py b/tests/prefetch_related/models.py index 08e62c26e0..68fdc2686d 100644 --- a/tests/prefetch_related/models.py +++ b/tests/prefetch_related/models.py @@ -65,7 +65,12 @@ class BookWithYear(Book): class Bio(models.Model): - author = models.OneToOneField(Author, models.CASCADE) + author = models.OneToOneField( + Author, + models.CASCADE, + primary_key=True, + to_field='name', + ) books = models.ManyToManyField(Book, blank=True) diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py index 85de08b184..36d60ea1f4 100644 --- a/tests/prefetch_related/tests.py +++ b/tests/prefetch_related/tests.py @@ -81,6 +81,23 @@ class PrefetchRelatedTests(TestCase): with self.assertRaises(BookWithYear.DoesNotExist): book.bookwithyear + def test_onetoone_reverse_with_to_field_pk(self): + """ + A model (Bio) with a OneToOneField primary key (author) that references + a non-pk field (name) on the related model (Author) is prefetchable. + """ + Bio.objects.bulk_create([ + Bio(author=self.author1), + Bio(author=self.author2), + Bio(author=self.author3), + ]) + authors = Author.objects.filter( + name__in=[self.author1, self.author2, self.author3], + ).prefetch_related('bio') + with self.assertNumQueries(2): + for author in authors: + self.assertEqual(author.name, author.bio.author.name) + def test_survives_clone(self): with self.assertNumQueries(2): [list(b.first_time_authors.all()) |