Fixed #28918 -- Fixed Model.refresh_from_db() for instances hidden by the default manager.

This commit is contained in:
Tim Graham 2017-12-30 18:00:36 -05:00 committed by GitHub
parent dcdd219ee1
commit d065c92678
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View File

@ -591,7 +591,7 @@ class Model(metaclass=ModelBase):
'are not allowed in fields.' % LOOKUP_SEP)
db = using if using is not None else self._state.db
db_instance_qs = self.__class__._default_manager.using(db).filter(pk=self.pk)
db_instance_qs = self.__class__._base_manager.using(db).filter(pk=self.pk)
# Use provided fields, if not set then reload all non-deferred fields.
deferred_fields = self.get_deferred_fields()

View File

@ -607,6 +607,16 @@ class CustomManagersRegressTestCase(TestCase):
# there would now be two objects in the database.
self.assertEqual(RestrictedModel.plain_manager.count(), 1)
def test_refresh_from_db_when_default_manager_filters(self):
"""
Model.refresh_from_db() works for instances hidden by the default
manager.
"""
book = Book._base_manager.create(is_published=False)
Book._base_manager.filter(pk=book.pk).update(title='Hi')
book.refresh_from_db()
self.assertEqual(book.title, 'Hi')
def test_delete_related_on_filtered_manager(self):
"""Deleting related objects should also not be distracted by a
restricted manager on the related object. This is a regression