Opened 11 years ago
Closed 8 months ago
#22977 closed Cleanup/optimization (fixed)
No validation error when ForeignKey related_name clashes with manager name.
| Reported by: | Russell Keith-Magee | Owned by: | Mariusz Felisiak |
|---|---|---|---|
| Component: | Core (System checks) | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Mariusz Felisiak | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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:
Author.authors.filter(name__startswith='Douglas')
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 (18)
comment:1 by , 11 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:2 by , 11 years ago
| Has patch: | set |
|---|
comment:3 by , 11 years ago
| Patch needs improvement: | set |
|---|
comment:4 by , 11 years ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
comment:5 by , 11 years ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
| Version: | 1.6 → master |
comment:6 by , 11 years ago
Given loic's comment at https://github.com/django/django/pull/3359#issuecomment-59642918, what would be the correct implementation of this validation check? Adding it to BaseManager.check method?
comment:7 by , 4 years ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
comment:8 by , 12 months ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:9 by , 12 months ago
| Cc: | added |
|---|
comment:10 by , 11 months ago
| Patch needs improvement: | unset |
|---|
comment:11 by , 11 months ago
| Patch needs improvement: | set |
|---|
comment:12 by , 8 months ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
follow-up: 15 comment:14 by , 8 months ago
Replying to Mariusz Felisiak:
I will finish it.
Hey Mariusz Felisiak are you still working on this? If not i would like to pick it up
comment:15 by , 8 months ago
Replying to Johanan Oppong Amoateng:
Replying to Mariusz Felisiak:
I will finish it.
Hey Mariusz Felisiak are you still working on this? If not i would like to pick it up
It's only 9 days, pretty early to ask. I'll finish it in the coming days.
comment:17 by , 8 months ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
As Loic noted on the PR, we should revisit this after the
_metafactor is merged.