8000 Fixed #36389 -- Fixed GenericRelation.update() use write DB connection. by JaeHyuckSa · Pull Request #19476 · django/django · GitHub
[go: up one dir, main page]

Skip to content

Fixed #36389 -- Fixed GenericRelation.update() use write DB connection. #19476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions django/contrib/contenttypes/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,11 @@ def _apply_rel_filters(self, queryset):
"""
Filter the queryset for the instance this manager is bound to.
"""
db = self._db or router.db_for_read(self.model, instance=self.instance)
return queryset.using(db).filter(**self.core_filters)
queryset._add_hints(instance=self.instance)
if self._db:
queryset = queryset.using(self._db)
queryset._defer_next_filter = True
return queryset.filter(**self.core_filters)
Comment on lines +614 to +615
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
queryset._defer_next_filter = True
return queryset.filter(**self.core_filters)
return queryset.filter(**self.core_filters)

I think I can remove _defer_next_filter without test failures

However, this is almost identical to ManyRelatedManager._apply_rel_filters except this would then be:

Suggested change
queryset._defer_next_filter = True
return queryset.filter(**self.core_filters)
queryset._defer_next_filter = True
return queryset._next_is_sticky().filter(**self.core_filters)

I am not sure what would be correct here but we can either simplify this or we should add more tests


def _remove_prefetched_objects(self):
try:
Expand Down
28 changes: 28 additions & 0 deletions tests/multiple_database/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2530,6 +2530,34 @@ def test_reverse_m2m_update(self):
self.assertEqual(e.model, Book)
self.assertEqual(e.hints, {"instance": auth})

def test_generic_rel_update(self):
book = Book.objects.create(
title="Pro Django", published=datetime.date(2008, 12, 16)
)
Review.objects.create(source="Python Monthly", content_object=book)

with self.assertRaises(RouterUsed) as cm:
with self.override_router():
book.reviews.update(source="Python Daily")
e = cm.exception
self.assertEqual(e.mode, RouterUsed.WRITE)
self.assertEqual(e.model, Review)
self.assertEqual(e.hints, {"instance": book})

def test_generic_rel_delete(self):
book = Book.objects.create(
title="Pro Django", published=datetime.date(2008, 12, 16)
)
Review.objects.create(source="Python Monthly", content_object=book)

with self.assertRaises(RouterUsed) as cm:
with self.override_router():
book.reviews.all().delete()
e = cm.exception
self.assertEqual(e.mode, RouterUsed.WRITE)
self.assertEqual(e.model, Review)
self.assertEqual(e.hints, {"instance": book})


class NoRelationRouter:
"""Disallow all relations."""
Expand Down
0