diff options
| author | Florian Apolloner <apollo13@users.noreply.github.com> | 2017-01-14 14:32:07 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-01-14 08:32:07 -0500 |
| commit | 84c1826ded17b2d74f66717fb745fc36e37949fd (patch) | |
| tree | 24b9e86375c400e670fd737c2619c013e665704c /docs/ref | |
| parent | 611ef422b173b450b1fc6f7f94eb262961b24e54 (diff) | |
Fixed #27718 -- Added QuerySet.union(), intersection(), difference().
Thanks Mariusz Felisiak for review and Oracle assistance.
Thanks Tim Graham for review and writing docs.
Diffstat (limited to 'docs/ref')
| -rw-r--r-- | docs/ref/models/querysets.txt | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 5fe783514d..f5f0fbcc8b 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -801,6 +801,61 @@ typically caches its results. If the data in the database might have changed since a ``QuerySet`` was evaluated, you can get updated results for the same query by calling ``all()`` on a previously evaluated ``QuerySet``. +``union()`` +~~~~~~~~~~~ + +.. method:: union(*other_qs, all=False) + +.. versionadded:: 1.11 + +Uses SQL's ``UNION`` operator to combine the results of two or more +``QuerySet``\s. For example: + + >>> qs1.union(qs2, qs3) + +The ``UNION`` operator selects only distinct values by default. To allow +duplicate values, use the ``all=True`` argument. + +``union()``, ``intersection()``, and ``difference()`` return model instances +of the type of the first ``QuerySet`` even if the arguments are ``QuerySet``\s +of other models. Passing different models works as long as the ``SELECT`` list +is the same in all ``QuerySet``\s (at least the types, the names don't matter +as long as the types in the same order). + +In addition, only ``LIMIT``, ``OFFSET``, and ``ORDER BY`` (i.e. slicing and +:meth:`order_by`) are allowed on the resulting ``QuerySet``. Further, databases +place restrictions on what operations are allowed in the combined queries. For +example, most databases don't allow ``LIMIT`` or ``OFFSET`` in the combined +queries. + +``intersection()`` +~~~~~~~~~~~~~~~~~~ + +.. method:: intersection(*other_qs) + +.. versionadded:: 1.11 + +Uses SQL's ``INTERSECT`` operator to return the shared elements of two or more +``QuerySet``\s. For example: + + >>> qs1.itersect(qs2, qs3) + +See :meth:`union` for some restrictions. + +``difference()`` +~~~~~~~~~~~~~~~~ + +.. method:: difference(*other_qs) + +.. versionadded:: 1.11 + +Uses SQL's ``EXCEPT`` operator to keep only elements present in the +``QuerySet`` but not in some other ``QuerySet``\s. For example:: + + >>> qs1.difference(qs2, qs3) + +See :meth:`union` for some restrictions. + ``select_related()`` ~~~~~~~~~~~~~~~~~~~~ |
