Opened 17 months ago

Last modified 10 months ago

#22977 assigned Cleanup/optimization

No validation error when ForeignKey related_name clashes with manager name.

Reported by: russellm Owned by: davide-ceretti
Component: Core (System checks) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no


Consider the following model definition:

class Author(models.Model):
    authors = models.Manager()

    name = models.CharField(max_length=100)

    mentor = models.ForeignKey('self', related_name='authors')

The related name on the the foreign key clashes with the manager, so when you try to issue a query:


you get an error because "Author.authors has no attribute 'filter'".

The problem is order dependent; if you define the manager *after* the foreign key, you get different errors.

I haven't checked what errors you get if you just have the default Manager, and a related_name of 'objects'.

There may also be problems if you have a field named 'objects'.

I suspect this class of problem could be picked up by the system check framework.

Change History (6)

comment:1 Changed 14 months ago by kswiat

  • Owner changed from nobody to kswiat
  • Status changed from new to assigned

comment:2 Changed 14 months ago by kswiat

  • Has patch set

comment:3 Changed 13 months ago by timgraham

  • Patch needs improvement set

As Loic noted on the PR, we should revisit this after the _meta factor is merged.

comment:4 Changed 13 months ago by kswiat

  • Owner kswiat deleted
  • Status changed from assigned to new

comment:5 Changed 10 months ago by davide-ceretti

  • Owner set to davide-ceretti
  • Status changed from new to assigned
  • Version changed from 1.6 to master

comment:6 Changed 10 months ago by davide-ceretti

Given loic's comment at, what would be the correct implementation of this validation check? Adding it to BaseManager.check method?

Note: See TracTickets for help on using tickets.
Back to Top