Skip to content

Commit

Permalink
Optimized Model.refresh_from_db(fields=...) by using a set.
Browse files Browse the repository at this point in the history
  • Loading branch information
g-nie authored and felixxm committed Mar 8, 2024
1 parent 0c690c6 commit 74f7fe3
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions django/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,8 +691,8 @@ def refresh_from_db(self, using=None, fields=None, from_queryset=None):
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)
Expand All @@ -717,11 +717,11 @@ def refresh_from_db(self, using=None, fields=None, from_queryset=None):
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()
Expand Down

0 comments on commit 74f7fe3

Please sign in to comment.