diff options
-rw-r--r-- | docs/changelog.rst | 1 | ||||
-rw-r--r-- | factory/django.py | 34 |
2 files changed, 27 insertions, 8 deletions
diff --git a/docs/changelog.rst b/docs/changelog.rst index 01d5775..24c01aa 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -21,6 +21,7 @@ ChangeLog - :issue:`201`: Properly handle custom Django managers when dealing with abstract Django models. - :issue:`212`: Fix :meth:`factory.django.mute_signals` to handle Django's signal caching + - :issue:`228`: Don't load :func:`django.apps.apps.get_model()` until required .. _v2.5.2: diff --git a/factory/django.py b/factory/django.py index b2af12c..b3c508c 100644 --- a/factory/django.py +++ b/factory/django.py @@ -56,16 +56,34 @@ def require_django(): raise import_failure -if django is None: - def get_model(app, model): - raise import_failure +_LAZY_LOADS = {} + +def get_model(app, model): + """Wrapper around django's get_model.""" + if 'get_model' not in _LAZY_LOADS: + _lazy_load_get_model() + + _get_model = _LAZY_LOADS['get_model'] + return _get_model(app, model) + + +def _lazy_load_get_model(): + """Lazy loading of get_model. + + get_model loads django.conf.settings, which may fail if + the settings haven't been configured yet. + """ + if django is None: + def get_model(app, model): + raise import_failure -elif django.VERSION[:2] < (1, 7): - from django.db.models.loading import get_model + elif django.VERSION[:2] < (1, 7): + from django.db.models.loading import get_model -else: - from django import apps as django_apps - get_model = django_apps.apps.get_model + else: + from django import apps as django_apps + get_model = django_apps.apps.get_model + _LAZY_LOADS['get_model'] = get_model class DjangoOptions(base.FactoryOptions): |