From 74f7fe3f3dd9725e92aa079c5f54b65d66db28de Mon Sep 17 00:00:00 2001 From: Giannis Terzopoulos Date: Fri, 8 Mar 2024 10:33:18 +0100 Subject: [PATCH] Optimized Model.refresh_from_db(fields=...) by using a set. --- django/db/models/base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index 9c8ab7bfa6..243093da44 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -691,8 +691,8 @@ 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): + fields = set(fields) + for field in fields.copy(): if field in prefetched_objects_cache: del prefetched_objects_cache[field] fields.remove(field) @@ -717,11 +717,11 @@ class Model(AltersData, metaclass=ModelBase): if fields is not None: db_instance_qs = db_instance_qs.only(*fields) elif deferred_fields: - fields = [ + fields = { f.attname for f in self._meta.concrete_fields if f.attname not in deferred_fields - ] + } db_instance_qs = db_instance_qs.only(*fields) db_instance = db_instance_qs.get()