Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions django/db/models/fields/related_descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def get_prefetch_querysets(self, instances, querysets=None):
"length of 1."
)
queryset = querysets[0]
queryset._add_hints(instance=instances[0])
else:
_cloning_disabled = True
queryset = self.get_queryset(instance=instances[0])._disable_cloning()
Expand Down Expand Up @@ -480,6 +481,7 @@ def get_prefetch_querysets(self, instances, querysets=None):
"length of 1."
)
queryset = querysets[0]
queryset._add_hints(instance=instances[0])
else:
_cloning_disabled = True
queryset = self.get_queryset(instance=instances[0])._disable_cloning()
Expand Down
1 change: 1 addition & 0 deletions django/db/models/fields/related_lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def get_prep_lookup(self):
-1
]
self.rhs = [target_field.get_prep_value(v) for v in self.rhs]
self.prepare_rhs = False
elif not getattr(self.rhs, "has_select_fields", True) and not getattr(
self.lhs.field.target_field, "primary_key", False
):
Expand Down
5 changes: 5 additions & 0 deletions docs/releases/6.1.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ Bugfixes
detect mixed ``on_delete`` variants for auto-created intermediate models for
``ManyToManyField``\s (:ticket:`37254`).

* Fixed a regression in Django 6.1 where custom querysets used with
:class:`~django.db.models.Prefetch` for forward foreign key or reverse
one-to-one relationships were not routed using the parent queryset's
database (:ticket:`37300`).

* Fixed a regression in Django 6.1 where HTML-safe strings, such as those
created with :func:`~django.utils.safestring.mark_safe`, used as form media
assets were treated as asset paths rather than being rendered verbatim
Expand Down
32 changes: 32 additions & 0 deletions tests/prefetch_related/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,38 @@ def test_using_is_honored_custom_qs(self):
)
self.assertEqual(books, "Poems ()\n" "Sense and Sensibility ()\n")

def test_forward_fk_prefetch_custom_queryset_uses_parent_database(self):
book1 = Book.objects.using("default").create(title="Dive into Python")
Author.objects.using("default").create(name="Mark", first_book=book1)
book2 = Book.objects.using("other").create(title="Dive into Rust")
Author.objects.using("other").create(name="Mark", first_book=book2)

author = (
Author.objects.using("other")
.prefetch_related(Prefetch("first_book", queryset=Book.objects.all()))
.get(name="Mark")
)

self.assertEqual(author._state.db, "other")
self.assertEqual(author.first_book._state.db, "other")
self.assertEqual(author.first_book.title, "Dive into Rust")

def test_reverse_o2o_prefetch_custom_queryset_uses_parent_database(self):
BookWithYear.objects.using("default").create(title="Poems", published_year=2000)
BookWithYear.objects.using("other").create(title="Poems", published_year=2001)

book = (
Book.objects.using("other")
.prefetch_related(
Prefetch("bookwithyear", queryset=BookWithYear.objects.all())
)
.get(title="Poems")
)

self.assertEqual(book._state.db, "other")
self.assertEqual(book.bookwithyear._state.db, "other")
self.assertEqual(book.bookwithyear.published_year, 2001)


class Ticket19607Tests(TestCase):
@classmethod
Expand Down
Loading