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 Tim Graham, 9 years ago

Summary: Queryset with 'values' and 'select_related' won't workAdd a friendly error message when using Queryset.select_related() after values()
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

We could add a friendly error message for that case. There's no purpose to using select_related() after values() because values() already selects only the values it needs.

comment:2 by Y3K, 9 years ago

Owner: changed from nobody to Y3K
Status: newassigned

I'll take this one.

Will update the ticket once I send the PR.

comment:3 by Y3K, 9 years ago

Easy pickings: set

comment:4 by Josh Smeaton <josh.smeaton@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In cbcf78fa:

Fixed #25252 -- Added friendly error message on incorrect .select_related() use

Using select_related() after .values() or .values_list() is not possible
because .values() or .values_list() already got the values they need.

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