summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2015-04-14 00:20:33 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2015-04-14 00:20:33 +0200
commite357919cdb52af96eb67148fd38dced34981821a (patch)
tree33d6fe6b767d885980a0395418b8a3af5f0c06df
parent16b414d27d638fd76701f10fe338c67d7d9dfde0 (diff)
downloadfactory-boy-e357919cdb52af96eb67148fd38dced34981821a.tar
factory-boy-e357919cdb52af96eb67148fd38dced34981821a.tar.gz
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.
-rw-r--r--factory/django.py17
-rw-r--r--tests/test_django.py4
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 = {}