diff --git a/django/db/models/query.py b/django/db/models/query.py
index 0f1d87c..526bc94 100644
a
|
b
|
class QuerySet(object):
|
42 | 42 | self._prefetch_related_lookups = [] |
43 | 43 | self._prefetch_done = False |
44 | 44 | self._known_related_object = None # (attname, rel_obj) |
| 45 | self._gimme_called = False |
45 | 46 | |
46 | 47 | ######################## |
47 | 48 | # PYTHON MAGIC METHODS # |
… |
… |
class QuerySet(object):
|
541 | 542 | else: |
542 | 543 | forced_managed = False |
543 | 544 | try: |
544 | | rows = query.get_compiler(self.db).execute_sql(None) |
| 545 | if self._gimme_called: |
| 546 | ret = query.get_compiler(self.db) |
| 547 | else: |
| 548 | ret = query.get_compiler(self.db).execute_sql(None) |
545 | 549 | if forced_managed: |
546 | 550 | transaction.commit(using=self.db) |
547 | 551 | else: |
… |
… |
class QuerySet(object):
|
550 | 554 | if forced_managed: |
551 | 555 | transaction.leave_transaction_management(using=self.db) |
552 | 556 | self._result_cache = None |
553 | | return rows |
| 557 | return ret |
554 | 558 | update.alters_data = True |
555 | 559 | |
556 | 560 | def _update(self, values): |
… |
… |
class QuerySet(object):
|
932 | 936 | return obj.query.get_compiler(connection=connection).as_nested_sql() |
933 | 937 | raise ValueError("Can't do subqueries with queries on different DBs.") |
934 | 938 | |
| 939 | def _gimme_qs(self): |
| 940 | """ |
| 941 | Calling this debug-helper method will give a queryset instead of |
| 942 | executing the query when calling a terminal queries. Usage example: |
| 943 | qs = qs._gimme_qs().update(foo='bar') |
| 944 | assertTrue('footable' in qs.query.alias_map) |
| 945 | |
| 946 | The terminal method must be called directly after _gimme_qs(). |
| 947 | """ |
| 948 | self._gimme_called = True |
| 949 | return self |
| 950 | |
935 | 951 | # When used as part of a nested query, a queryset will never be an "always |
936 | 952 | # empty" result. |
937 | 953 | value_annotation = True |
diff --git a/tests/modeltests/update/tests.py b/tests/modeltests/update/tests.py
index 7a1177c..4b9da1f 100644
a
|
b
|
|
1 | 1 | from __future__ import absolute_import, unicode_literals |
2 | 2 | |
| 3 | from django.db.models import Count |
3 | 4 | from django.test import TestCase |
4 | 5 | |
5 | 6 | from .models import A, B, C, D, DataPoint, RelatedPoint |
… |
… |
class AdvancedTests(TestCase):
|
116 | 117 | method = DataPoint.objects.all()[:2].update |
117 | 118 | self.assertRaises(AssertionError, method, |
118 | 119 | another_value='another thing') |
| 120 | |
| 121 | def test_update_annotate(self): |
| 122 | qs = DataPoint.objects.annotate(Count('relatedpoint__name'))._gimme_qs().update(value='foo') |
| 123 | print qs.query |