Changeset 7499
- Timestamp:
- 04/28/08 09:14:41 (1 year ago)
- Files:
-
- django/trunk/django/db/models/query.py (modified) (2 diffs)
- django/trunk/django/db/models/sql/query.py (modified) (1 diff)
- django/trunk/docs/db-api.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/db/models/query.py
r7497 r7499 29 29 ######################## 30 30 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 31 42 def __repr__(self): 32 43 return repr(list(self)) … … 38 49 if self._result_cache is None: 39 50 if self._iter: 40 self._result_cache = list(self._iter ())51 self._result_cache = list(self._iter) 41 52 else: 42 53 self._result_cache = list(self.iterator()) django/trunk/django/db/models/sql/query.py
r7493 r7499 99 99 memo[id(self)] = result 100 100 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 101 119 102 120 def get_meta(self): django/trunk/docs/db-api.txt
r7489 r7499 376 376 iterating over a ``QuerySet`` will take advantage of your database to 377 377 load data and instantiate objects only as you need them. 378 379 380 Pickling QuerySets 381 ~~~~~~~~~~~~~~~~~~ 382 383 If you pickle_ a ``QuerySet``, this will also force all the results to be 384 loaded into memory prior to pickling. This is because pickling is usually used 385 as a precursor to caching and when the cached queryset is reloaded, you want 386 the 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 388 than the results that are currently in the database. 389 390 If you only want to pickle the necessary information to recreate the 391 ``Queryset`` from the database at a later time, pickle the ``query`` attribute 392 of the ``QuerySet``. You can then recreate the original ``QuerySet`` (without 393 any 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 378 401 379 402 Limiting QuerySets
