Django

Code

Show
Ignore:
Timestamp:
04/28/08 09:14:41 (2 months ago)
Author:
mtredinnick
Message:

Added the ability to pickle and unpickle QuerySets? and Query classes.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/db-api.txt

    r7489 r7499  
    376376      iterating over a ``QuerySet`` will take advantage of your database to 
    377377      load data and instantiate objects only as you need them. 
     378 
     379 
     380Pickling QuerySets 
     381~~~~~~~~~~~~~~~~~~ 
     382 
     383If you pickle_ a ``QuerySet``, this will also force all the results to be 
     384loaded into memory prior to pickling. This is because pickling is usually used 
     385as a precursor to caching and when the cached queryset is reloaded, you want 
     386the results to already be present. This means that when you unpickle a 
     387``QuerySet``, it contains the results at the moment it was pickled, rather 
     388than the results that are currently in the database. 
     389 
     390If you only want to pickle the necessary information to recreate the 
     391``Queryset`` from the database at a later time, pickle the ``query`` attribute 
     392of the ``QuerySet``. You can then recreate the original ``QuerySet`` (without 
     393any results loaded) using some code like this:: 
     394 
     395    >>> import pickle 
     396    >>> query = pickle.loads(s)     # Assuming 's' is the pickled string. 
     397    >>> qs = MyModel.objects.all() 
     398    >>> qs.query = query            # Restore the original 'query'. 
     399 
     400.. _pickle: http://docs.python.org/lib/module-pickle.html 
    378401 
    379402Limiting QuerySets