Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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

Closed
wants to merge 1 commit into from

2 participants

Florian Apolloner
errx commented June 18, 2012

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

Florian Apolloner
Owner

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

Florian Apolloner apollo13 closed this January 01, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jun 19, 2012
Fixed #18414 -- Raise assertion with exists() and sliced query 3a2a48f
This page is out of date. Refresh to see the latest.
2  django/db/models/query.py
@@ -569,6 +569,8 @@ def _update(self, values):
569 569
     _update.alters_data = True
570 570
 
571 571
     def exists(self):
  572
+        assert self.query.can_filter(),\
  573
+                "Cannot check existance once a slice has been taken."
572 574
         if self._result_cache is None:
573 575
             return self.query.has_results(using=self.db)
574 576
         return bool(self._result_cache)
9  tests/regressiontests/queries/tests.py
@@ -1580,12 +1580,21 @@ def test_ticket8597(self):
1580 1580
 class ExistsSql(TestCase):
1581 1581
     def setUp(self):
1582 1582
         settings.DEBUG = True
  1583
+        Article.objects.create(name='one', created=datetime.datetime.now())
1583 1584
 
1584 1585
     def test_exists(self):
1585 1586
         self.assertFalse(Tag.objects.exists())
1586 1587
         # Ok - so the exist query worked - but did it include too many columns?
1587 1588
         self.assertTrue("id" not in connection.queries[-1]['sql'] and "name" not in connection.queries[-1]['sql'])
1588 1589
 
  1590
+    def test_ticket_18414(self):
  1591
+        self.assertEqual(Article.objects.all().exists(), True)
  1592
+        self.assertRaisesMessage(
  1593
+            AssertionError,
  1594
+            'Cannot check existance once a slice has been taken.',
  1595
+            Article.objects.all()[:1].exists
  1596
+        )
  1597
+
1589 1598
     def tearDown(self):
1590 1599
         settings.DEBUG = False
1591 1600
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.