#36973: fields.E348 check should check against the accessor_name instead of the
model name
-------------------------------------+-------------------------------------
     Reporter:  nick.ma@…            |                     Type:  Bug
       Status:  new                  |                Component:  Core
                                     |  (System checks)
      Version:  6.0                  |                 Severity:  Normal
     Keywords:  System checks,       |             Triage Stage:
  fields.E348, related_name          |  Unreviewed
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  1                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 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(
  }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36973>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019cc13ac69e-eb2b1cce-f28b-4b4e-9e59-54f97caeda33-000000%40eu-central-1.amazonses.com.

Reply via email to