Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add some state management methods to AppCache.

  • Loading branch information...
commit 7e81213b5a4570926afbd67eff7f2675f636d720 1 parent b546e7e
Andrew Godwin authored

Showing 1 changed file with 31 additions and 0 deletions. Show diff stats Hide diff stats

  1. 31  django/db/models/loading.py
31  django/db/models/loading.py
@@ -244,6 +244,37 @@ def register_models(self, app_label, *models):
244 244
             model_dict[model_name] = model
245 245
         self._get_models_cache.clear()
246 246
 
  247
+    def save_state(self):
  248
+        """
  249
+        Returns an object that contains the current AppCache state.
  250
+        Can be provided to restore_state to undo actions.
  251
+        """
  252
+        return {
  253
+            "app_store": SortedDict(self.app_store.items()),
  254
+            "app_labels": dict(self.app_errors.items()),
  255
+            "app_models": SortedDict(self.app_models.items()),
  256
+            "app_errors": dict(self.app_errors.items()),
  257
+        }
  258
+
  259
+    def restore_state(self, state):
  260
+        """
  261
+        Restores the AppCache to a previous state from save_state.
  262
+        """
  263
+        self.app_store = state['app_store']
  264
+        self.app_labels = state['app_labels']
  265
+        self.app_models = state['app_models']
  266
+        self.app_errors = state['app_errors']
  267
+
  268
+    def unregister_all(self):
  269
+        """
  270
+        Wipes the AppCache clean of all registered models.
  271
+        Used for things like migration libraries' fake ORMs.
  272
+        """
  273
+        self.app_store = SortedDict()
  274
+        self.app_labels = {}
  275
+        self.app_models = SortedDict()
  276
+        self.app_errors = {}
  277
+
247 278
 cache = AppCache()
248 279
 
249 280
 # These methods were always module level, so are kept that way for backwards

0 notes on commit 7e81213

Please sign in to comment.
Something went wrong with that request. Please try again.