Skip to content


Fixed #18414 -- Raise assertion with exists() and sliced query #161

wants to merge 1 commit into from

2 participants


Assertion that query hasn't been sliced when calling exists() method.

Django member

Can you open a new pull request with the comments from the ticket? Thx!

@apollo13 apollo13 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 18, 2012
Showing with 11 additions and 0 deletions.
  1. +2 −0 django/db/models/
  2. +9 −0 tests/regressiontests/queries/
2 django/db/models/
@@ -569,6 +569,8 @@ def _update(self, values):
_update.alters_data = True
def exists(self):
+ assert self.query.can_filter(),\
+ "Cannot check existance once a slice has been taken."
if self._result_cache is None:
return self.query.has_results(using=self.db)
return bool(self._result_cache)
9 tests/regressiontests/queries/
@@ -1580,12 +1580,21 @@ def test_ticket8597(self):
class ExistsSql(TestCase):
def setUp(self):
settings.DEBUG = True
+ Article.objects.create(name='one',
def test_exists(self):
# Ok - so the exist query worked - but did it include too many columns?
self.assertTrue("id" not in connection.queries[-1]['sql'] and "name" not in connection.queries[-1]['sql'])
+ def test_ticket_18414(self):
+ self.assertEqual(Article.objects.all().exists(), True)
+ self.assertRaisesMessage(
+ AssertionError,
+ 'Cannot check existance once a slice has been taken.',
+ Article.objects.all()[:1].exists
+ )
def tearDown(self):
settings.DEBUG = False
Something went wrong with that request. Please try again.