Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added app labels to related fields checks messages E302-E305. #13819

Merged
merged 2 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions django/db/models/fields/related.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,16 @@ def _check_clashes(self):
rel_is_hidden = self.remote_field.is_hidden()
rel_name = self.remote_field.get_accessor_name() # i. e. "model_set"
rel_query_name = self.related_query_name() # i. e. "model"
field_name = "%s.%s" % (opts.object_name, self.name) # i. e. "Model.field"
# i.e. "app_label.Model.field".
field_name = '%s.%s' % (opts.label, self.name)

# Check clashes between accessor or reverse query name of `field`
# and any other field name -- i.e. accessor for Model.foreign is
# model_set and it clashes with Target.model_set.
potential_clashes = rel_opts.fields + rel_opts.many_to_many
for clash_field in potential_clashes:
clash_name = "%s.%s" % (rel_opts.object_name, clash_field.name) # i.e. "Target.model_set"
# i.e. "app_label.Target.model_set".
clash_name = '%s.%s' % (rel_opts.label, clash_field.name)
if not rel_is_hidden and clash_field.name == rel_name:
errors.append(
checks.Error(
Expand Down Expand Up @@ -253,9 +255,11 @@ def _check_clashes(self):
# Model.m2m accessor.
potential_clashes = (r for r in rel_opts.related_objects if r.field is not self)
for clash_field in potential_clashes:
clash_name = "%s.%s" % ( # i. e. "Model.m2m"
clash_field.related_model._meta.object_name,
clash_field.field.name)
# i.e. "app_label.Model.m2m".
clash_name = '%s.%s' % (
clash_field.related_model._meta.label,
clash_field.field.name,
)
if not rel_is_hidden and clash_field.get_accessor_name() == rel_name:
errors.append(
checks.Error(
Expand Down
21 changes: 11 additions & 10 deletions docs/ref/checks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,17 @@ Related fields

* **fields.E300**: Field defines a relation with model ``<model>``, which is
either not installed, or is abstract.
* **fields.E301**: Field defines a relation with the model ``<model>`` which
has been swapped out.
* **fields.E302**: Accessor for field ``<field name>`` clashes with field
``<field name>``.
* **fields.E303**: Reverse query name for field ``<field name>`` clashes with
field ``<field name>``.
* **fields.E304**: Field name ``<field name>`` clashes with accessor for
``<field name>``.
* **fields.E305**: Field name ``<field name>`` clashes with reverse query name
for ``<field name>``.
* **fields.E301**: Field defines a relation with the model
``<app_label>.<model>`` which has been swapped out.
* **fields.E302**: Accessor for field ``<app_label>.<model>.<field name>``
clashes with field ``<app_label>.<model>.<field name>``.
* **fields.E303**: Reverse query name for field
``<app_label>.<model>.<field name>`` clashes with field
``<app_label>.<model>.<field name>``.
* **fields.E304**: Field name ``<app_label>.<model>.<field name>`` clashes with
accessor for ``<app_label>.<model>.<field name>``.
* **fields.E305**: Field name ``<app_label>.<model>.<field name>`` clashes with
reverse query name for ``<app_label>.<model>.<field name>``.
* **fields.E306**: Related name must be a valid Python identifier or end with
a ``'+'``.
* **fields.E307**: The field ``<app label>.<model>.<field name>`` was declared
Expand Down