mirror of https://github.com/django/django.git
Fixed #34137 -- Made Model.refresh_from_db() clear cached generic relations.
Thanks Simon Charette for the implementation idea.
This commit is contained in:
parent
5eab4d1924
commit
123b1d3fcf
|
@ -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):
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue