Django

Code

Changeset 7499

Show
Ignore:
Timestamp:
04/28/08 09:14:41 (1 year 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/django/db/models/query.py

    r7497 r7499  
    2929    ######################## 
    3030 
     31    def __getstate__(self): 
     32        """ 
     33        Allows the Queryset to be pickled. 
     34        """ 
     35        # Force the cache to be fully populated. 
     36        len(self) 
     37 
     38        obj_dict = self.__dict__.copy() 
     39        obj_dict['_iter'] = None 
     40        return obj_dict 
     41 
    3142    def __repr__(self): 
    3243        return repr(list(self)) 
     
    3849        if self._result_cache is None: 
    3950            if self._iter: 
    40                 self._result_cache = list(self._iter()
     51                self._result_cache = list(self._iter
    4152            else: 
    4253                self._result_cache = list(self.iterator()) 
  • django/trunk/django/db/models/sql/query.py

    r7493 r7499  
    9999        memo[id(self)] = result 
    100100        return result 
     101 
     102    def __getstate__(self): 
     103        """ 
     104        Pickling support. 
     105        """ 
     106        obj_dict = self.__dict__.copy() 
     107        del obj_dict['connection'] 
     108        return obj_dict 
     109 
     110    def __setstate__(self, obj_dict): 
     111        """ 
     112        Unpickling support. 
     113        """ 
     114        self.__dict__.update(obj_dict) 
     115        # XXX: Need a better solution for this when multi-db stuff is 
     116        # supported. It's the only class-reference to the module-level 
     117        # connection variable. 
     118        self.connection = connection 
    101119 
    102120    def get_meta(self): 
  • 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