Fixed #35236 -- Used Field.attname/column attributes instead of get_attname()/get_attname_column().

This commit is contained in:
Adam Johnson 2024-02-19 22:32:55 +00:00 committed by Mariusz Felisiak
parent e0496b2e9a
commit 31314980be
8 changed files with 13 additions and 18 deletions

View File

@ -618,7 +618,7 @@ class ChangeList:
else:
if isinstance(field.remote_field, ManyToOneRel):
# <FK>_id field names don't require a join.
if field_name != field.get_attname():
if field_name != field.attname:
return True
return False

View File

@ -187,7 +187,7 @@ class GenericForeignKey(FieldCacheMixin, Field):
fk_dict = defaultdict(set)
# We need one instance for each group in order to get the right db:
instance_dict = {}
ct_attname = self.model._meta.get_field(self.ct_field).get_attname()
ct_attname = self.model._meta.get_field(self.ct_field).attname
for instance in instances:
# We avoid looking for values if either ct_id or fkey value is None
ct_id = getattr(instance, ct_attname)
@ -240,7 +240,7 @@ class GenericForeignKey(FieldCacheMixin, Field):
# content type ID here, and later when the actual instance is needed,
# use ContentType.objects.get_for_id(), which has a global cache.
f = self.model._meta.get_field(self.ct_field)
ct_id = getattr(instance, f.get_attname(), None)
ct_id = getattr(instance, f.attname, None)
pk_val = getattr(instance, self.fk_field)
rel_obj = self.get_cached_value(instance, default=None)

View File

@ -67,10 +67,10 @@ class BaseGenericInlineFormSet(BaseModelFormSet):
def save_new(self, form, commit=True):
setattr(
form.instance,
self.ct_field.get_attname(),
self.ct_field.attname,
ContentType.objects.get_for_model(self.instance).pk,
)
setattr(form.instance, self.ct_fk_field.get_attname(), self.instance.pk)
setattr(form.instance, self.ct_fk_field.attname, self.instance.pk)
return form.save(commit=commit)

View File

@ -106,7 +106,7 @@ class Serializer(base.Serializer):
differently from regular fields).
"""
self._start_relational_field(field)
related_att = getattr(obj, field.get_attname())
related_att = getattr(obj, field.attname)
if related_att is not None:
if self.use_natural_foreign_keys and hasattr(
field.remote_field.model, "natural_key"

View File

@ -1925,7 +1925,7 @@ class Model(AltersData, metaclass=ModelBase):
errors = []
for f in cls._meta.local_fields:
_, column_name = f.get_attname_column()
column_name = f.column
# Ensure the column name is not already in use.
if column_name and column_name in used_column_names:
@ -1972,7 +1972,7 @@ class Model(AltersData, metaclass=ModelBase):
errors = []
property_names = cls._meta._property_names
related_field_accessors = (
f.get_attname()
f.attname
for f in cls._meta._get_fields(reverse=False)
if f.is_relation and f.related_model is not None
)
@ -2320,13 +2320,11 @@ class Model(AltersData, metaclass=ModelBase):
return errors
for f in cls._meta.local_fields:
_, column_name = f.get_attname_column()
# Check if auto-generated name for the field is too long
# for the database.
if (
f.db_column is None
and column_name is not None
and (column_name := f.column) is not None
and len(column_name) > allowed_len
):
errors.append(
@ -2348,10 +2346,9 @@ class Model(AltersData, metaclass=ModelBase):
# Check if auto-generated name for the M2M field is too long
# for the database.
for m2m in f.remote_field.through._meta.local_fields:
_, rel_name = m2m.get_attname_column()
if (
m2m.db_column is None
and rel_name is not None
and (rel_name := m2m.column) is not None
and len(rel_name) > allowed_len
):
errors.append(

View File

@ -1204,8 +1204,7 @@ class RawSQL(Expression):
if query.model:
for parent in query.model._meta.get_parent_list():
for parent_field in parent._meta.local_fields:
_, column_name = parent_field.get_attname_column()
if column_name.lower() in self.sql.lower():
if parent_field.column.lower() in self.sql.lower():
query.resolve_ref(
parent_field.name, allow_joins, reuse, summarize
)

View File

@ -2186,8 +2186,7 @@ class RawQuerySet:
converter = connections[self.db].introspection.identifier_converter
model_fields = {}
for field in self.model._meta.fields:
name, column = field.get_attname_column()
model_fields[converter(column)] = field
model_fields[converter(field.column)] = field
return model_fields

View File

@ -1140,7 +1140,7 @@ class BaseInlineFormSet(BaseModelFormSet):
if self.fk.remote_field.field_name != self.fk.remote_field.model._meta.pk.name:
fk_value = getattr(self.instance, self.fk.remote_field.field_name)
fk_value = getattr(fk_value, "pk", fk_value)
setattr(form.instance, self.fk.get_attname(), fk_value)
setattr(form.instance, self.fk.attname, fk_value)
return form
@classmethod