mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Fixed #28375 -- Fixed KeyError crash on reverse prefetch of a model with OneToOneField primary key to a non-pk field.
This commit is contained in:
parent
b5ad5c628a
commit
fea9cb46aa
3 changed files with 25 additions and 8 deletions
|
@ -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())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue