From 1d77b931f7df96ec4d6fa3835fe35547397cca1a Mon Sep 17 00:00:00 2001 From: sarahboyce Date: Thu, 29 Sep 2022 20:19:38 +0200 Subject: [PATCH] Refs #32095 -- Added model's Options._non_pk_concrete_field_names property. --- django/db/models/base.py | 10 +--------- django/db/models/options.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index 1f7e5f84aa3..cb51ed42c46 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -799,15 +799,7 @@ class Model(metaclass=ModelBase): return update_fields = frozenset(update_fields) - field_names = set() - - for field in self._meta.concrete_fields: - if not field.primary_key: - field_names.add(field.name) - - if field.name != field.attname: - field_names.add(field.attname) - + field_names = self._meta._non_pk_concrete_field_names non_model_fields = update_fields.difference(field_names) if non_model_fields: diff --git a/django/db/models/options.py b/django/db/models/options.py index bff55011dc8..b6b82028022 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -89,6 +89,7 @@ class Options: "many_to_many", "concrete_fields", "local_concrete_fields", + "_non_pk_concrete_field_names", "_forward_fields_map", "managers", "managers_map", @@ -982,6 +983,19 @@ class Options: names.append(name) return frozenset(names) + @cached_property + def _non_pk_concrete_field_names(self): + """ + Return a set of the non-pk concrete field names defined on the model. + """ + names = [] + for field in self.concrete_fields: + if not field.primary_key: + names.append(field.name) + if field.name != field.attname: + names.append(field.attname) + return frozenset(names) + @cached_property def db_returning_fields(self): """