1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

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 '
'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()

View File

@ -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))