diff --git a/django/db/models/base.py b/django/db/models/base.py
index a5c9986..30b12da 100644
|
a
|
b
|
class Model(object):
|
| 467 | 467 | if pk_set: |
| 468 | 468 | # Determine whether a record with the primary key already exists. |
| 469 | 469 | if (force_update or (not force_insert and |
| 470 | | manager.filter(pk=pk_val).extra(select={'a': 1}).values('a').order_by())): |
| | 470 | manager.filter(pk=pk_val).exists())): |
| 471 | 471 | # It does already exist, so do an UPDATE. |
| 472 | 472 | if force_update or non_pks: |
| 473 | 473 | values = [(f, None, (raw and getattr(self, f.attname) or f.pre_save(self, False))) for f in non_pks] |
diff --git a/django/db/models/manager.py b/django/db/models/manager.py
index 52612d8..73d2bf5 100644
|
a
|
b
|
class Manager(object):
|
| 172 | 172 | |
| 173 | 173 | def only(self, *args, **kwargs): |
| 174 | 174 | return self.get_query_set().only(*args, **kwargs) |
| | 175 | |
| | 176 | def exists(self, *args, **kwargs): |
| | 177 | return self.get_query_ste().exists(*args, **kwargs) |
| 175 | 178 | |
| 176 | 179 | def _insert(self, values, **kwargs): |
| 177 | 180 | return insert_query(self.model, values, **kwargs) |
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 46a86fc..02288c1 100644
|
a
|
b
|
class QuerySet(object):
|
| 443 | 443 | self._result_cache = None |
| 444 | 444 | return query.execute_sql(None) |
| 445 | 445 | _update.alters_data = True |
| | 446 | |
| | 447 | def exists(self): |
| | 448 | if self._result_cache is None: |
| | 449 | return self.query.has_results() |
| | 450 | return bool(self._result_cache) |
| 446 | 451 | |
| 447 | 452 | ################################################## |
| 448 | 453 | # PUBLIC METHODS THAT RETURN A QUERYSET SUBCLASS # |
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 23f99e4..1a66507 100644
|
a
|
b
|
class BaseQuery(object):
|
| 383 | 383 | number = min(number, self.high_mark - self.low_mark) |
| 384 | 384 | |
| 385 | 385 | return number |
| | 386 | |
| | 387 | def has_results(self): |
| | 388 | """ |
| | 389 | Returns whether or not there are any items in the result set this Query |
| | 390 | represents. Does this in a super tricking way. Basically the |
| | 391 | equivalent of QuerySet.extra(select={'a': 1}).values('a').order_by() |
| | 392 | to maximize efficiency. |
| | 393 | """ |
| | 394 | c = self.clone() |
| | 395 | c.add_extra({'a': 1}, None, None, None, None, None) |
| | 396 | c.add_fields(()) |
| | 397 | c.set_extra_mask(('a',)) |
| | 398 | c.set_aggregate_mask(()) |
| | 399 | c.clear_ordering() |
| | 400 | results = c.execute_sql() |
| | 401 | return bool(list(results)) |
| | 402 | |
| 386 | 403 | |
| 387 | 404 | def as_sql(self, with_limits=True, with_col_aliases=False): |
| 388 | 405 | """ |
diff --git a/django/forms/models.py b/django/forms/models.py
index cc43612..37fce68 100644
|
a
|
b
|
class BaseModelForm(BaseForm):
|
| 319 | 319 | if self.instance.pk is not None: |
| 320 | 320 | qs = qs.exclude(pk=self.instance.pk) |
| 321 | 321 | |
| 322 | | # This cute trick with extra/values is the most efficient way to |
| 323 | | # tell if a particular query returns any results. |
| 324 | | if qs.extra(select={'a': 1}).values('a').order_by(): |
| | 322 | if qs.exists(): |
| 325 | 323 | if len(unique_check) == 1: |
| 326 | 324 | self._errors[unique_check[0]] = ErrorList([self.unique_error_message(unique_check)]) |
| 327 | 325 | else: |
| … |
… |
class BaseModelForm(BaseForm):
|
| 354 | 352 | if self.instance.pk is not None: |
| 355 | 353 | qs = qs.exclude(pk=self.instance.pk) |
| 356 | 354 | |
| 357 | | # This cute trick with extra/values is the most efficient way to |
| 358 | | # tell if a particular query returns any results. |
| 359 | | if qs.extra(select={'a': 1}).values('a').order_by(): |
| | 355 | if qs.exists(): |
| 360 | 356 | self._errors[field] = ErrorList([ |
| 361 | 357 | self.date_error_message(lookup_type, field, unique_for) |
| 362 | 358 | ]) |
diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt
index efd7c54..26c76ca 100644
|
a
|
b
|
Aggregation <topics-db-aggregation>`.
|
| 1114 | 1114 | |
| 1115 | 1115 | .. _field-lookups: |
| 1116 | 1116 | |
| | 1117 | ``exists()`` |
| | 1118 | ~~~~~~~~~~~~ |
| | 1119 | |
| | 1120 | Returns whether or not there are any items in the ``QuerySet``. This will |
| | 1121 | perform the query in the most efficient way possible. |
| | 1122 | |
| 1117 | 1123 | Field lookups |
| 1118 | 1124 | ------------- |
| 1119 | 1125 | |