#23732 closed Bug (fixed)
select_related() documentation examples use incorrect fieldnames, and are slightly misleading
Reported by: | Daniele Procida | Owned by: | Tim Graham |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.select_related says that if you have the models
class City(models.Model): # ... pass class Person(models.Model): # ... hometown = models.ForeignKey(City) class Book(models.Model): # ... author = models.ForeignKey(Person)
then you can do a Book.objects.select_related('person__city').get(id=4)
.
But in fact that, and the other examples that follow, should be using select_related('author__hometown')
(the names of the fields, rather than of the models they refer to).
These examples should be changed.
A related issue in the same section: the examples might lead one to believe that select_related
works only when retrieving a single item with get()
(unlike prefetch_related
, whose examples involve retrieving multiple objects), whereas this is not in fact the case.
An example using filter()
or some more explicit wording in the description (or both) would help here.
Attachments (1)
Change History (6)
comment:1 by , 10 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
by , 10 years ago
Attachment: | 23732.diff added |
---|
comment:3 by , 10 years ago
Has patch: | set |
---|
comment:4 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
In e958c760f90672e6d1422fb5509f64c12527a451: