Fixed #34137 -- Made Model.refresh_from_db() clear cached generic relations.

Thanks Simon Charette for the implementation idea.
This commit is contained in:
Bhuvnesh 2022-11-07 12:36:30 +05:30 committed by GitHub
parent 5eab4d1924
commit 123b1d3fcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

View File

@ -737,6 +737,11 @@ class Model(AltersData, metaclass=ModelBase):
if field.is_cached(self):
field.delete_cached_value(self)
# Clear cached private relations.
for field in self._meta.private_fields:
if field.is_relation and field.is_cached(self):
field.delete_cached_value(self)
self._state.db = db_instance._state.db
async def arefresh_from_db(self, using=None, fields=None):

View File

@ -43,6 +43,14 @@ class GenericForeignKeyTests(TestCase):
self.assertIsNone(post.parent)
self.assertIsNone(post.parent)
def test_clear_cached_generic_relation(self):
question = Question.objects.create(text="What is your name?")
answer = Answer.objects.create(text="Answer", question=question)
old_entity = answer.question
answer.refresh_from_db()
new_entity = answer.question
self.assertIsNot(old_entity, new_entity)
class GenericRelationTests(TestCase):
def test_value_to_string(self):