Skip to content

fix: Use proper column name instead of attname#573

Merged
foarsitter merged 4 commits intojazzband:masterfrom
last-partizan:patch-1
Jun 16, 2023
Merged

fix: Use proper column name instead of attname#573
foarsitter merged 4 commits intojazzband:masterfrom
last-partizan:patch-1

Conversation

@last-partizan
Copy link
Contributor

@last-partizan last-partizan commented May 30, 2023

From django comment in db/models/fields/__init__.py:

# A guide to Field parameters:
#
#   * name:      The name of the field specified in the model.
#   * attname:   The attribute to use on the model object. This is the same as
#                "name", except in the case of ForeignKeys, where "_id" is
#                appended.
#   * db_column: The db_column specified in the model (or None).
#   * column:    The database column for this field. This is the same as
#                "attname", except if db_column is specified.
#
# Code that introspects values, or does other dynamic things, should use
# attname. For example, this gets the primary key value of object "obj":
#
#     getattr(obj, opts.pk.attname)

Problem

Currently there is not problem, unless someone used db_column on their parent link field.

Solution

This changes attribute name following django recommendations.

From django comment in db/models/fields/__init__.py:

# A guide to Field parameters:
#
#   * name:      The name of the field specified in the model.
#   * attname:   The attribute to use on the model object. This is the same as
#                "name", except in the case of ForeignKeys, where "_id" is
#                appended.
#   * db_column: The db_column specified in the model (or None).
#   * column:    The database column for this field. This is the same as
#                "attname", except if db_column is specified.
#
# Code that introspects values, or does other dynamic things, should use
# attname. For example, this gets the primary key value of object "obj":
#
#     getattr(obj, opts.pk.attname)
@foarsitter
Copy link
Contributor

Do you think you can provide a test-case for the scenario you are describing?

@last-partizan
Copy link
Contributor Author

Sure, updated test cases.

@foarsitter
Copy link
Contributor

Thanks for providing a test case. I confirmed the issue and fix locally.

Can you update CHANGES.rst?

@codecov
Copy link

codecov bot commented Jun 16, 2023

Codecov Report

Merging #573 (695b242) into master (6916342) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master     #573   +/-   ##
=======================================
  Coverage   95.12%   95.12%           
=======================================
  Files           6        6           
  Lines         820      820           
=======================================
  Hits          780      780           
  Misses         40       40           
Impacted Files Coverage Δ
model_utils/managers.py 98.08% <ø> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@foarsitter foarsitter merged commit 0ec4ac5 into jazzband:master Jun 16, 2023
@foarsitter
Copy link
Contributor

Thanks for your time and contribution @last-partizan!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants