Commits

Andrew Godwin committed bb2fb9c

Lazily construct the FakeORMs.

  • Participants
  • Parent commits a629e34

Comments (0)

Files changed (2)

south/migration.py

 
 from south.models import MigrationHistory
 from south.db import db
-from south.orm import FakeORM
+from south.orm import LazyFakeORM, FakeORM
 from south.signals import *
 
 def get_app(app):
     try:
         module = __import__(app.__name__ + "." + name, '', '', ['Migration'])
         migclass = module.Migration
-        migclass.orm = FakeORM(migclass, get_app_name(app))
+        migclass.orm = LazyFakeORM(migclass, get_app_name(app))
         module._ = lambda x: x  # Fake i18n
         module.datetime = datetime
         return migclass
     """
     if not args in _orm_cache:
         _orm_cache[args] = _FakeORM(*args)  
-    return _orm_cache[args] 
+    return _orm_cache[args]
+
+
+class LazyFakeORM(object):
+    """
+    In addition to memoising the ORM call, this function lazily generates them
+    for a Migration class. Assign the result of this to (for example)
+    .orm, and as soon as .orm is accessed the ORM will be created.
+    """
+    
+    def __init__(self, *args):
+        self._args = args
+        self.orm = None
+    
+    def __get__(self, obj, type=None):
+        if not self.orm:
+            self.orm = FakeORM(*self._args)
+        return self.orm
 
 
 class _FakeORM(object):