diff options
| author | Ryan Heard <ryanwheard@gmail.com> | 2021-07-02 15:09:13 -0500 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-03-04 12:55:37 +0100 |
| commit | c6b4d62fa2c7f73b87f6ae7e8cf1d64ee5312dc5 (patch) | |
| tree | 238bd8c3045c8d4577e09bd913de9748dcd49968 /docs/ref | |
| parent | 795da6306a048b18c0158496b0d49e8e4f197a32 (diff) | |
Fixed #29865 -- Added logical XOR support for Q() and querysets.
Diffstat (limited to 'docs/ref')
| -rw-r--r-- | docs/ref/models/querysets.txt | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 44950ecf52..8311b63f7e 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -1903,6 +1903,40 @@ SQL equivalent: ``|`` is not a commutative operation, as different (though equivalent) queries may be generated. +XOR (``^``) +~~~~~~~~~~~ + +.. versionadded:: 4.1 + +Combines two ``QuerySet``\s using the SQL ``XOR`` operator. + +The following are equivalent:: + + Model.objects.filter(x=1) ^ Model.objects.filter(y=2) + from django.db.models import Q + Model.objects.filter(Q(x=1) ^ Q(y=2)) + +SQL equivalent: + +.. code-block:: sql + + SELECT ... WHERE x=1 XOR y=2 + +.. note:: + + ``XOR`` is natively supported on MariaDB and MySQL. On other databases, + ``x ^ y ^ ... ^ z`` is converted to an equivalent: + + .. code-block:: sql + + (x OR y OR ... OR z) AND + 1=( + (CASE WHEN x THEN 1 ELSE 0 END) + + (CASE WHEN y THEN 1 ELSE 0 END) + + ... + (CASE WHEN z THEN 1 ELSE 0 END) + + ) + Methods that do not return ``QuerySet``\s ----------------------------------------- @@ -3751,8 +3785,12 @@ A ``Q()`` object represents an SQL condition that can be used in database-related operations. It's similar to how an :class:`F() <django.db.models.F>` object represents the value of a model field or annotation. They make it possible to define and reuse conditions, and -combine them using operators such as ``|`` (``OR``) and ``&`` (``AND``). See -:ref:`complex-lookups-with-q`. +combine them using operators such as ``|`` (``OR``), ``&`` (``AND``), and ``^`` +(``XOR``). See :ref:`complex-lookups-with-q`. + +.. versionchanged:: 4.1 + + Support for the ``^`` (``XOR``) operator was added. ``Prefetch()`` objects ---------------------- |
