db.models.query.QuerySet.reverse()

reverse() 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

db.models.query.QuerySet.order_by()

order_by(*fields) By default, results returned by a QuerySet are ordered by the ordering tuple given by the ordering option in the model’s Meta. You can override this on a per-QuerySet basis by using the order_by method. Example: Entry.objects.filter(pub_date__year=2005).order_by('-pub_date', 'headline') The result above will be ordered by pub_date descending, then by headline ascending. The negative sign in front of "-pub_date" indicates descending order. Ascending order is implied. To ord

db.models.query.QuerySet.ordered

ordered True if the QuerySet is ordered — i.e. has an order_by() clause or a default ordering on the model. False otherwise.

db.models.query.QuerySet.only()

only(*fields) The only() method is more or less the opposite of defer(). You call it with the fields that should not be deferred when retrieving a model. If you have a model where almost all the fields need to be deferred, using only() to specify the complementary set of fields can result in simpler code. Suppose you have a model with fields name, age and biography. The following two querysets are the same, in terms of deferred fields: Person.objects.defer("age", "biography") Person.objects.

db.models.query.QuerySet.none()

none() Calling none() will create a queryset that never returns any objects and no query will be executed when accessing the results. A qs.none() queryset is an instance of EmptyQuerySet. Examples: >>> Entry.objects.none() <QuerySet []> >>> from django.db.models.query import EmptyQuerySet >>> isinstance(Entry.objects.none(), EmptyQuerySet) True

db.models.query.QuerySet.last()

last() Works like first(), but returns the last object in the queryset.

db.models.query.QuerySet.latest()

latest(field_name=None) Returns the latest object in the table, by date, using the field_name provided as the date field. This example returns the latest Entry in the table, according to the pub_date field: Entry.objects.latest('pub_date') If your model’s Meta specifies get_latest_by, you can leave off the field_name argument to earliest() or latest(). Django will use the field specified in get_latest_by by default. Like get(), earliest() and latest() raise DoesNotExist if there is no objec

db.models.query.QuerySet.in_bulk()

in_bulk(id_list=None) Takes a list of primary-key values and returns a dictionary mapping each primary-key value to an instance of the object with the given ID. If a list isn’t provided, all objects in the queryset are returned. Example: >>> Blog.objects.in_bulk([1]) {1: <Blog: Beatles Blog>} >>> Blog.objects.in_bulk([1, 2]) {1: <Blog: Beatles Blog>, 2: <Blog: Cheddar Talk>} >>> Blog.objects.in_bulk([]) {} >>> Blog.objects.in_bulk() {1: &

db.models.query.QuerySet.get_or_create()

get_or_create(defaults=None, **kwargs) A convenience method for looking up an object with the given kwargs (may be empty if your model has defaults for all fields), creating one if necessary. Returns a tuple of (object, created), where object is the retrieved or created object and created is a boolean specifying whether a new object was created. This is meant as a shortcut to boilerplatish code. For example: try: obj = Person.objects.get(first_name='John', last_name='Lennon') except Pers

db.models.query.QuerySet.iterator()

iterator() Evaluates the QuerySet (by performing the query) and returns an iterator (see PEP 234) over the results. A QuerySet typically caches its results internally so that repeated evaluations do not result in additional queries. In contrast, iterator() will read results directly, without doing any caching at the QuerySet level (internally, the default iterator calls iterator() and caches the return value). For a QuerySet which returns a large number of objects that you only need to acces