Opened 9 years ago

Closed 7 years ago

#2150 closed enhancement (fixed)

slicing to the end of a QuerySet

Reported by: Gary Wilson <gary.wilson@…> Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: normal Keywords: qs-rf-fixed
Cc: gary.wilson@…, ferringb@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Currently, when trying to slice to the end of a QuerySet, an AssertionError is raised:

>>>Foo.objects.all()[3:]
Traceback (most recent call last):
  File "<console>", line 1, in ?
  File "/usr/lib/python2.4/site-packages/django/db/models/query.py", line 88, in __repr__
    return repr(self._get_data())
  File "/usr/lib/python2.4/site-packages/django/db/models/query.py", line 412, in _get_data
    self._result_cache = list(self.iterator())
  File "/usr/lib/python2.4/site-packages/django/db/models/query.py", line 162, in iterator
    select, sql, params = self._get_sql_clause()
  File "/usr/lib/python2.4/site-packages/django/db/models/query.py", line 490, in _get_sql_clause
    assert self._offset is None, "'offset' is not allowed without 'limit'"
AssertionError: 'offset' is not allowed without 'limit'

However, returning rows from an offset to the end of a result set does seem possible...

in MySQL (>=3.23)

"To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:"

http://dev.mysql.com/doc/refman/4.1/en/select.html

SELECT * FROM tbl LIMIT 95,18446744073709551615;

and in PostgreSQL (>=7.3)

"LIMIT ALL is the same as omitting the LIMIT clause."

http://www.postgresql.org/docs/7.3/static/queries-limit.html

SELECT select_list
    FROM table_expression
    [LIMIT { number | ALL }] [OFFSET number]

Change History (7)

comment:1 Changed 9 years ago by Gary Wilson <gary.wilson@…>

  • Cc gary.wilson@… added

comment:2 Changed 8 years ago by Michael Radziej <mir@…>

  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 8 years ago by (removed)

  • Cc ferringb@… added

comment:4 Changed 7 years ago by jacob

  • Keywords qs-rf added
  • Triage Stage changed from Design decision needed to Accepted

comment:5 Changed 7 years ago by mtredinnick

(In [7147]) queryset-refactor: Implemented slicing to end of querysets.

Refs #2150, #5012.

comment:6 Changed 7 years ago by mtredinnick

  • Keywords qs-rf-fixed added; qs-rf removed

comment:7 Changed 7 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

(In [7477]) Merged the queryset-refactor branch into trunk.

This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.

Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658

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