Fixed #28974 -- Made refresh_from_db() hint routers about its instance.

This commit is contained in:
Simon Charette 2018-01-02 09:42:24 -05:00 committed by Tim Graham
parent 0f5a741ada
commit f5a989e603
2 changed files with 11 additions and 2 deletions

View File

@ -590,8 +590,8 @@ class Model(metaclass=ModelBase):
'Found "%s" in fields argument. Relations and transforms ' 'Found "%s" in fields argument. Relations and transforms '
'are not allowed in fields.' % LOOKUP_SEP) 'are not allowed in fields.' % LOOKUP_SEP)
db = using if using is not None else self._state.db hints = {'instance': self}
db_instance_qs = self.__class__._base_manager.using(db).filter(pk=self.pk) 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. # Use provided fields, if not set then reload all non-deferred fields.
deferred_fields = self.get_deferred_fields() deferred_fields = self.get_deferred_fields()

View File

@ -2,6 +2,7 @@ import datetime
import pickle import pickle
from io import StringIO from io import StringIO
from operator import attrgetter from operator import attrgetter
from unittest.mock import Mock
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType 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.title, "Dive into Python (on default)")
self.assertEqual(dive._state.db, "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): def test_basic_queries(self):
"Queries are constrained to a single database" "Queries are constrained to a single database"
dive = Book.objects.using('other').create(title="Dive into Python", published=datetime.date(2009, 5, 4)) dive = Book.objects.using('other').create(title="Dive into Python", published=datetime.date(2009, 5, 4))