Skip to content

Commit

Permalink
Fixed #35236 -- Used Field.attname/column attributes instead of get_a…
Browse files Browse the repository at this point in the history
…ttname()/get_attname_column().
  • Loading branch information
adamchainz authored and felixxm committed Feb 20, 2024
1 parent e0496b2 commit 3131498
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion django/contrib/admin/views/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ def has_related_field_in_list_display(self):
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

Expand Down
4 changes: 2 additions & 2 deletions django/contrib/contenttypes/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def get_prefetch_querysets(self, instances, querysets=None):
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)
Expand Down Expand Up @@ -240,7 +240,7 @@ def __get__(self, instance, cls=None):
# 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)
Expand Down
4 changes: 2 additions & 2 deletions django/contrib/contenttypes/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ def get_default_prefix(cls):
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)


Expand Down
2 changes: 1 addition & 1 deletion django/core/serializers/xml_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def handle_fk_field(self, obj, field):
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"
Expand Down
11 changes: 4 additions & 7 deletions django/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1925,7 +1925,7 @@ def _check_column_name_clashes(cls):
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:
Expand Down Expand Up @@ -1972,7 +1972,7 @@ def _check_property_name_related_field_accessor_clashes(cls):
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
)
Expand Down Expand Up @@ -2320,13 +2320,11 @@ def _check_long_column_names(cls, databases):
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(
Expand All @@ -2348,10 +2346,9 @@ def _check_long_column_names(cls, databases):
# 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(
Expand Down
3 changes: 1 addition & 2 deletions django/db/models/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,8 +1204,7 @@ def resolve_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
)
Expand Down
3 changes: 1 addition & 2 deletions django/db/models/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2186,8 +2186,7 @@ def model_fields(self):
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


Expand Down
2 changes: 1 addition & 1 deletion django/forms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ def _construct_form(self, i, **kwargs):
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
Expand Down

0 comments on commit 3131498

Please sign in to comment.