Opened 9 years ago
Closed 9 years ago
#25252 closed Cleanup/optimization (fixed)
Add a friendly error message when using Queryset.select_related() after values()
Reported by: | 2roy999 | Owned by: | Y3K |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.8 |
Severity: | Normal | Keywords: | queryset values select_related |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
Trying to use a queryset while using both methods 'values' and 'select_related' will throw an error when trying to use iter.
models:
class Poll(models.Model): name = models.CharField(max_length=50L) class Entry(models.Model): poll = models.ForeignKey(Poll) name = models.CharField(max_length=50L)
code:
>>> iter(Entry.objects.values('name').select_related('poll'))
error:
Traceback (most recent call last): File "<console>", line 1, in <module> File "/*path_to_project*/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 162, in __iter__ self._fetch_all() File "/*path_to_project*/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 965, in _fetch_all self._result_cache = list(self.iterator()) File "/*path_to_project*/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 1085, in iterator for row in self.query.get_compiler(self.db).results_iter(): File "/*path_to_project*/env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 794, in results_iter results = self.execute_sql(MULTI) File "/*path_to_project*/env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 829, in execute_sql sql, params = self.as_sql() File "/*path_to_project*/env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 378, in as_sql extra_select, order_by, group_by = self.pre_sql_setup() File "/*path_to_project*/env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 48, in pre_sql_setup self.setup_query() File "/*path_to_project*/env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 39, in setup_query self.select, self.klass_info, self.annotation_col_map = self.get_select() File "/*path_to_project*/env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 207, in get_select klass_info['related_klass_infos'] = related_klass_infos TypeError: 'NoneType' object does not support item assignment
Change History (4)
comment:1 by , 9 years ago
Summary: | Queryset with 'values' and 'select_related' won't work → Add a friendly error message when using Queryset.select_related() after values() |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
comment:2 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
I'll take this one.
Will update the ticket once I send the PR.
comment:3 by , 9 years ago
Easy pickings: | set |
---|
Github pullrequest: https://github.com/django/django/pull/5210
Note:
See TracTickets
for help on using tickets.
We could add a friendly error message for that case. There's no purpose to using
select_related()
aftervalues()
becausevalues()
already selects only the values it needs.