diff --git a/django/db/models/base.py b/django/db/models/base.py index e2becf8bafd0..6eaa600f107b 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -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] @@ -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 = [ diff --git a/tests/basic/tests.py b/tests/basic/tests.py index c6ad1faefda5..ad82cffe8c21 100644 --- a/tests/basic/tests.py +++ b/tests/basic/tests.py @@ -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])