Opened 95 minutes ago
Last modified 61 minutes ago
#36973 assigned Bug
fields.E348 check should check against the accessor_name instead of the model name
| Reported by: | Owned by: | Shubh Rai | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 6.0 |
| Severity: | Normal | Keywords: | System checks, fields.E348, related_name |
| Cc: | nick.ma@… | Triage Stage: | Unreviewed |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | yes | UI/UX: | no |
Description
when running manage.py I hit erroneous fields.E348 check. The system check is supposed to examine if ManyToOne/ManyToMany field <related_name> for <model>.<field name> clashes with the name of a model manager. However, instead of checking the related_name (i.e. <fieldname>_set or manually set related_name=<related_name>) it gets the actual field name of the foreign key.
As shown in the original check function below
def _check_conflict_with_managers(self):
errors = []
manager_names = {manager.name for manager in self.opts.managers}
for rel_objs in self.model._meta.related_objects:
related_object_name = rel_objs.name
if related_object_name in manager_names:
field_name = f"{self.model._meta.object_name}.{self.name}"
errors.append(
checks.Error(
f"Related name '{related_object_name}' for '{field_name}' "
"clashes with the name of a model manager.",
hint=(
"Rename the model manager or change the related_name "
f"argument in the definition for field '{field_name}'."
),
obj=self,
id="fields.E348",
)
)
return errors
it compares a model's managers' name and its related object field name.
related_object_name = rel_objs.name
Patch:
It should use the .accessor_name instead of .name
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index f1bc664007..a3ef912a8e 100644
--- a/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -723,7 +723,7 @@ class ForeignObject(RelatedField):
errors = []
manager_names = {manager.name for manager in self.opts.managers}
for rel_objs in self.model._meta.related_objects:
- related_object_name = rel_objs.name
+ related_object_name = rel_objs.accessor_name
if related_object_name in manager_names:
field_name = f"{self.model._meta.object_name}.{self.name}"
errors.append(
Change History (1)
comment:1 by , 61 minutes ago
| Component: | Core (System checks) → Database layer (models, ORM) |
|---|---|
| Owner: | set to |
| Status: | new → assigned |