Opened 9 years ago

Closed 9 years ago

#4293 closed (invalid)

The bug occur in a slice filter. |slice:"1::2" if query set is empty

Reported by: blinded.python@… Owned by: adrian
Component: Template system Version: 0.96
Severity: Keywords: loop state
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


{% for film in films|slice:"1::2" %}

The films is an empty query set.

Exception Type: ProgrammingError
Exception Value: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1' at line 1")

Change History (2)

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

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

I have not confirmed this, but it appears that the slice filter code is not catching AssertionError, which can be raised when an offset is specified without a limit. From django/db/models/

        # LIMIT and OFFSET clauses
        if self._limit is not None:
            sql.append("%s " % backend.get_limit_offset_sql(self._limit, self._offset))
            assert self._offset is None, "'offset' is not allowed without 'limit'"

If this is indeed the case, then this ticket should be changed to make the slice filter fail silently since there is already a ticket for slicing to the end of a QuerySet (#2150).

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

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

Well, this is the traceback I get whether the QuerySet is empty or not:

Traceback (most recent call last):
File "/home/gdub/bzr/django/main/django/template/" in render_node
  741. result = node.render(context)
File "/home/gdub/bzr/django/main/django/template/" in render
  96. values = self.sequence.resolve(context, True)
File "/home/gdub/bzr/django/main/django/template/" in resolve
  597. obj = func(obj, *arg_vals)
File "/home/gdub/bzr/django/main/django/template/" in slice_
  354. return value[slice(*bits)]
File "/home/gdub/bzr/django/main/django/db/models/" in __getitem__
  148. return list(self._clone(_offset=offset, _limit=limit))[::k.step]
File "/home/gdub/bzr/django/main/django/db/models/" in __iter__
  108. return iter(self._get_data())
File "/home/gdub/bzr/django/main/django/db/models/" in _get_data
  470. self._result_cache = list(self.iterator())
File "/home/gdub/bzr/django/main/django/db/models/" in iterator
  174. select, sql, params = self._get_sql_clause()
File "/home/gdub/bzr/django/main/django/db/models/" in _get_sql_clause
  550. assert self._offset is None, "'offset' is not allowed without 'limit'"

  AssertionError at /lab/items/
  'offset' is not allowed without 'limit'

but now that I think of it, we probably don't want this error to fail silently since it's an unsupported feature with QuerySets, and is not really the fault of the filter.

My error doesn't appear to be the same one you are seeing. If you could, please post more information. The full traceback, the query you are assigning to films, and the generated SQL would be great. Without that, closing this ticket as invalid as I am unable to reproduce the error you are seeing.

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