Skip to content

Commit

Permalink
Refs #34925 -- Avoided altering passed by reference refresh_from_db(f…
Browse files Browse the repository at this point in the history
…ields).

Follow up to b0ec87b.
  • Loading branch information
charettes authored Nov 25, 2023
1 parent b0ec87b commit 978680d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion django/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ def refresh_from_db(self, using=None, fields=None):
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]
Expand All @@ -711,7 +712,6 @@ def refresh_from_db(self, using=None, fields=None):
# 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 = [
Expand Down
4 changes: 3 additions & 1 deletion tests/basic/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,9 @@ def test_prefetched_cache_cleared(self):
# 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])

0 comments on commit 978680d

Please sign in to comment.