From f5a989e60394ac11f5dce886468a34533caefcbf Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Tue, 2 Jan 2018 09:42:24 -0500 Subject: [PATCH] Fixed #28974 -- Made refresh_from_db() hint routers about its instance. --- django/db/models/base.py | 4 ++-- tests/multiple_database/tests.py | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index 5633fff89a..19648fbed9 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -590,8 +590,8 @@ class Model(metaclass=ModelBase): 'Found "%s" in fields argument. Relations and transforms ' 'are not allowed in fields.' % LOOKUP_SEP) - db = using if using is not None else self._state.db - db_instance_qs = self.__class__._base_manager.using(db).filter(pk=self.pk) + hints = {'instance': self} + db_instance_qs = self.__class__._base_manager.db_manager(using, hints=hints).filter(pk=self.pk) # Use provided fields, if not set then reload all non-deferred fields. deferred_fields = self.get_deferred_fields() diff --git a/tests/multiple_database/tests.py b/tests/multiple_database/tests.py index 50859a3dc6..ab88ecf875 100644 --- a/tests/multiple_database/tests.py +++ b/tests/multiple_database/tests.py @@ -2,6 +2,7 @@ import datetime import pickle from io import StringIO from operator import attrgetter +from unittest.mock import Mock from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType @@ -103,6 +104,14 @@ class QueryTestCase(TestCase): self.assertEqual(dive.title, "Dive into Python (on default)") self.assertEqual(dive._state.db, "default") + def test_refresh_router_instance_hint(self): + router = Mock() + router.db_for_read.return_value = None + book = Book.objects.create(title='Dive Into Python', published=datetime.date(1957, 10, 12)) + with self.settings(DATABASE_ROUTERS=[router]): + book.refresh_from_db() + router.db_for_read.assert_called_once_with(Book, instance=book) + def test_basic_queries(self): "Queries are constrained to a single database" dive = Book.objects.using('other').create(title="Dive into Python", published=datetime.date(2009, 5, 4))