From e357919cdb52af96eb67148fd38dced34981821a Mon Sep 17 00:00:00 2001 From: Raphaƫl Barrois Date: Tue, 14 Apr 2015 00:20:33 +0200 Subject: Remove warnings with Django 1.7 (Closes #195). Builds upon pull request by @shinuza: - Properly import ``get_model`` - Run ``django.setup()`` before importing any models. --- factory/django.py | 17 +++++++++++++++-- tests/test_django.py | 4 ++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/factory/django.py b/factory/django.py index cbf7c10..74e4fdb 100644 --- a/factory/django.py +++ b/factory/django.py @@ -32,8 +32,10 @@ import functools """factory_boy extensions for use with the Django framework.""" try: + import django from django.core import files as django_files except ImportError as e: # pragma: no cover + django = None django_files = None import_failure = e @@ -54,6 +56,18 @@ def require_django(): raise import_failure +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 + +else: + from django import apps as django_apps + get_model = django_apps.apps.get_model + + class DjangoOptions(base.FactoryOptions): def _build_default_options(self): return super(DjangoOptions, self)._build_default_options() + [ @@ -92,8 +106,7 @@ class DjangoModelFactory(base.Factory): if is_string(definition) and '.' in definition: app, model = definition.split('.', 1) - from django.db.models import loading as django_loading - return django_loading.get_model(app, model) + return get_model(app, model) return definition diff --git a/tests/test_django.py b/tests/test_django.py index 9da99cc..113caeb 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -50,6 +50,8 @@ from . import tools if django is not None: os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tests.djapp.settings') + if django.VERSION >= (1, 7, 0): + django.setup() from django import test as django_test from django.conf import settings from django.db import models as django_models @@ -64,8 +66,6 @@ if django is not None: else: django_test = unittest -if django is not None and django.VERSION >= (1, 7, 0): - django.setup() test_state = {} -- cgit v1.2.3