1
0
mirror of https://github.com/django/django.git synced 2025-01-03 15:06:09 +00:00

Refs #34925 -- Avoided altering passed by reference refresh_from_db(fields).

Follow up to b0ec87b857.
This commit is contained in:
Simon Charette 2023-11-25 11:01:14 -05:00 committed by GitHub
parent b0ec87b857
commit 978680db22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 2 deletions

View File

@ -691,6 +691,7 @@ class Model(AltersData, metaclass=ModelBase):
self._prefetched_objects_cache = {}
else:
prefetched_objects_cache = getattr(self, "_prefetched_objects_cache", ())
fields = list(fields)
for field in list(fields):
if field in prefetched_objects_cache:
del prefetched_objects_cache[field]
@ -711,7 +712,6 @@ class Model(AltersData, metaclass=ModelBase):
# Use provided fields, if not set then reload all non-deferred fields.
deferred_fields = self.get_deferred_fields()
if fields is not None:
fields = list(fields)
db_instance_qs = db_instance_qs.only(*fields)
elif deferred_fields:
fields = [

View File

@ -951,7 +951,9 @@ class ModelRefreshTests(TestCase):
# Relation is added and prefetch cache is stale.
self.assertCountEqual(a2_prefetched.selfref_set.all(), [])
self.assertCountEqual(a2_prefetched.cited.all(), [])
a2_prefetched.refresh_from_db(fields=["selfref_set", "cited"])
fields = ["selfref_set", "cited"]
a2_prefetched.refresh_from_db(fields=fields)
self.assertEqual(fields, ["selfref_set", "cited"])
# Cache was cleared and new results are available.
self.assertCountEqual(a2_prefetched.selfref_set.all(), [s])
self.assertCountEqual(a2_prefetched.cited.all(), [s])