#15962 closed Bug (invalid)
RelatedField.related_query_name() returns the wrong name if the default related_name has not been overridden
| Reported by: | Baptiste Mispelon | Owned by: | nobody | 
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev | 
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | yes | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | no | UI/UX: | no | 
Description
As an example, using the following models:
class Author(models.Model):
    pass
class Book(models.Model):
    author = models.ForeignKey(Author)
Doing Author._meta.get_field_by_name('book_set') throws a FieldDoesNotExists error.
To get the field, you currently have to do get_field_by_name('book').
The attached patch corrects the related_query_name method of django.db.models.fields.RelatedField to use the same logic as django.db.models.RelatedObject.get_accessor_name().
Attachments (1)
Change History (5)
by , 15 years ago
| Attachment: | 15962.diff added | 
|---|
follow-up: 2 comment:1 by , 14 years ago
| Resolution: | → invalid | 
|---|---|
| Status: | new → closed | 
comment:2 by , 14 years ago
I'm not sure what I'm doing wrong then but on a fresh install of trunk, I still get the error:
>>> Author._meta.get_field_by_name('book_set') 
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/me/.virtualenvs/bug15962/src/django/django/db/models/options.py", line 306, in get_field_by_name
    % (self.object_name, name))
FieldDoesNotExist: Author has no field named 'book_set'
But what you say about related_query_name and get_accessor_name beeing different makes sense.
I ran the test suite with my patch and I get some failures indeed (though only 5): it seems to break things like Permission.objects.filter(group__user=user_obj) for contrib.auth.
What confused me was the fact that if the field's related_name is set, then both methods return it and I therefore assumed they should also return the same value when related_name was not set.
follow-up: 4 comment:3 by , 14 years ago
Oops, look like I confused myself messing around with your patch or some stale .pyc files or something, because I'm getting that failure now.
Although this is a kind of inconsistency that would be nice to change, it's not a bug, and not something we can change without very big backwards incompatibility problems. So I'm leaving it as closed.
Using exactly your models, this works for me:
>>> models.Author._meta.get_field_by_name('book_set') (<RelatedObject: ticket15962:book related to author>, None, False, False)Also, note that the
related_query_nameis the name used in queries. Theget_accessor_nameis the attribute on objects that is used to return theQuerySetof related objects. These are not necessarily the same (especially forManyToManyfields), and they should not be changed to make them the same as each other. (Huge number of test suite failures result if you do).