db.models.query.QuerySet.prefetch_related()

prefetch_related(*lookups) Returns a QuerySet that will automatically retrieve, in a single batch, related objects for each of the specified lookups. This has a similar purpose to select_related, in that both are designed to stop the deluge of database queries that is caused by accessing related objects, but the strategy is quite different. select_related works by creating an SQL join and including the fields of the related object in the SELECT statement. For this reason, select_related gets

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.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.last()

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

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

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