mirror of https://github.com/django/django.git
Re-added docs for QuerySet reverse() and all() methods, refs #9000 - thanks ramiro
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9005 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
0cd7fbec56
commit
f17f2bfd1f
|
@ -7,9 +7,9 @@ QuerySet API reference
|
||||||
.. currentmodule:: django.db.models
|
.. currentmodule:: django.db.models
|
||||||
|
|
||||||
This document describes the details of the ``QuerySet`` API. It builds on the
|
This document describes the details of the ``QuerySet`` API. It builds on the
|
||||||
material presented in the :ref:`model <topics-db-models>` and `database query
|
material presented in the :ref:`model <topics-db-models>` and :ref:`database
|
||||||
<topics-db-queries>` guides, so you'll probably want to read and understand
|
query <topics-db-queries>` guides, so you'll probably want to read and
|
||||||
those documents before reading this one.
|
understand those documents before reading this one.
|
||||||
|
|
||||||
Throughout this reference we'll use the :ref:`example weblog models
|
Throughout this reference we'll use the :ref:`example weblog models
|
||||||
<queryset-model-example>` presented in the :ref:`database query guide
|
<queryset-model-example>` presented in the :ref:`database query guide
|
||||||
|
@ -192,6 +192,26 @@ There's no way to specify whether ordering should be case sensitive. With
|
||||||
respect to case-sensitivity, Django will order results however your database
|
respect to case-sensitivity, Django will order results however your database
|
||||||
backend normally orders them.
|
backend normally orders them.
|
||||||
|
|
||||||
|
``reverse()``
|
||||||
|
~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. versionadded:: 1.0
|
||||||
|
|
||||||
|
Use the ``reverse()`` method to reverse the order in which a queryset's
|
||||||
|
elements are returned. Calling ``reverse()`` a second time restores the
|
||||||
|
ordering back to the normal direction.
|
||||||
|
|
||||||
|
To retrieve the ''last'' five items in a queryset, you could do this::
|
||||||
|
|
||||||
|
my_queryset.reverse()[:5]
|
||||||
|
|
||||||
|
Note that this is not quite the same as slicing from the end of a sequence in
|
||||||
|
Python. The above example will return the last item first, then the
|
||||||
|
penultimate item and so on. If we had a Python sequence and looked at
|
||||||
|
``seq[-5:]``, we would see the fifth-last item first. Django doesn't support
|
||||||
|
that mode of access (slicing from the end), because it's not possible to do it
|
||||||
|
efficiently in SQL.
|
||||||
|
|
||||||
Also, note that ``reverse()`` should generally only be called on a
|
Also, note that ``reverse()`` should generally only be called on a
|
||||||
``QuerySet`` which has a defined ordering (e.g., when querying against
|
``QuerySet`` which has a defined ordering (e.g., when querying against
|
||||||
a model which defines a default ordering, or when using
|
a model which defines a default ordering, or when using
|
||||||
|
@ -200,7 +220,6 @@ a model which defines a default ordering, or when using
|
||||||
ordering was undefined prior to calling ``reverse()``, and will remain
|
ordering was undefined prior to calling ``reverse()``, and will remain
|
||||||
undefined afterward).
|
undefined afterward).
|
||||||
|
|
||||||
|
|
||||||
``distinct()``
|
``distinct()``
|
||||||
~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@ -393,6 +412,17 @@ Examples::
|
||||||
>>> Entry.objects.none()
|
>>> Entry.objects.none()
|
||||||
[]
|
[]
|
||||||
|
|
||||||
|
``all()``
|
||||||
|
~~~~~~~~~~
|
||||||
|
|
||||||
|
.. versionadded:: 1.0
|
||||||
|
|
||||||
|
Returns a ''copy'' of the current ``QuerySet`` (or ``QuerySet`` subclass you
|
||||||
|
pass in). This can be useful in some situations where you might want to pass
|
||||||
|
in either a model manager or a ``QuerySet`` and do further filtering on the
|
||||||
|
result. You can safely call ``all()`` on either object and then you'll
|
||||||
|
definitely have a ``QuerySet`` to work with.
|
||||||
|
|
||||||
.. _select-related:
|
.. _select-related:
|
||||||
|
|
||||||
``select_related()``
|
``select_related()``
|
||||||
|
|
Loading…
Reference in New Issue