diff options
| author | Selwin Ong <selwin.ong@gmail.com> | 2013-05-21 18:35:12 +0300 |
|---|---|---|
| committer | Anssi Kääriäinen <akaariai@gmail.com> | 2013-05-21 18:52:28 +0300 |
| commit | ea9a0857d4922fab1f9146f3a7828b67281edc89 (patch) | |
| tree | 0e4b9751c8731a839f1dfeb06f6d63dc25b05eda /docs | |
| parent | d595b61acae91e6111ec26cb0a4a1b1a9f4eb0d5 (diff) | |
Fixed #19326 -- Added first() and last() methods to QuerySet
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/ref/models/querysets.txt | 30 | ||||
| -rw-r--r-- | docs/releases/1.6.txt | 5 |
2 files changed, 35 insertions, 0 deletions
diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 2dec00afc1..b9a4037440 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -1585,6 +1585,36 @@ earliest Works otherwise like :meth:`~django.db.models.query.QuerySet.latest` except the direction is changed. +first +~~~~~ +.. method:: first() + +.. versionadded:: 1.6 + +Returns the first object matched by the queryset, or ``None`` if there +is no matching object. If the ``QuerySet`` has no ordering defined, then the +queryset is automatically ordered by the primary key. + +Example:: + + p = Article.objects.order_by('title', 'pub_date').first() + +Note that ``first()`` is a convenience method, the following code sample is +equivalent to the above example:: + + try: + p = Article.objects.order_by('title', 'pub_date')[0] + except IndexError: + p = None + +last +~~~~ +.. method:: last() + +.. versionadded:: 1.6 + +Works like :meth:`first()` except the ordering is reversed. + aggregate ~~~~~~~~~ diff --git a/docs/releases/1.6.txt b/docs/releases/1.6.txt index a417e81f62..29b4569d4e 100644 --- a/docs/releases/1.6.txt +++ b/docs/releases/1.6.txt @@ -253,6 +253,11 @@ Minor features allow users to specify the primary keys of objects they want to dump. This option can only be used with one model. +* Added ``QuerySet`` methods :meth:`~django.db.models.query.QuerySet.first` + and :meth:`~django.db.models.query.QuerySet.last` which are convenience + methods returning the first or last object matching the filters. Returns + ``None`` if there are no objects matching. + Backwards incompatible changes in 1.6 ===================================== |
