Ticket #20513: 20513.diff

File 20513.diff, 1.7 KB (added by seddonym, 2 years ago)
  • docs/topics/db/queries.txt

    diff --git a/docs/topics/db/queries.txt b/docs/topics/db/queries.txt
    index 39ef2b3..c3cf3c0 100644
    a b To avoid this problem, simply save the 
    747747    >>> print([p.headline for p in queryset]) # Evaluate the query set.
    748748    >>> print([p.pub_date for p in queryset]) # Re-use the cache from the evaluation.
    749749
     750When querysets are not cached
     751~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     752
     753Querysets do not always cache their results.  When evaluating only *part* of the queryset, the cache is checked, but if it is not present then the items returned are not cached.
     754
     755Specifically, this means that :ref:`limiting the queryset <limiting-querysets>` using an array slice or an index will not populate the cache.
     756
     757For example, repeatedly getting a certain index in a queryset object will requery the database each time::
     758
     759    >>> queryset = Entry.objects.all()
     760    >>> print queryset[5] # Queries the database
     761    >>> print queryset[5] # Queries the database again
     762
     763However, if the entire queryset has already been evaluated, the cache will be checked instead::
     764
     765    >>> queryset = Entry.objects.all()
     766    >>> [entry for entry in queryset] # Queries the database
     767    >>> print queryset[5] # Uses cache
     768    >>> print queryset[5] # Uses cache
     769
     770.. note::
     771
     772    Simply printing the queryset will not populate the cache.  This is because the call to ``__repr__()`` only returns a slice of the entire Queryset.
     773
     774Examples of actions that will populate the cache::
     775
     776    >>> [entry for entry in queryset]
     777    >>> bool(queryset)
     778    >>> entry in queryset
     779    >>> list(queryset)
     780
    750781.. _complex-lookups-with-q:
    751782
    752783Complex lookups with Q objects
Back to Top