Permalink
Browse files

Updates for Django 1.7: locate models in the new app registry

  • Loading branch information...
1 parent e6ad8ea commit 78208df2254adcfed12751303db32c40ea41545e @wolfe wolfe committed Feb 8, 2014
Showing with 31 additions and 8 deletions.
  1. +22 −8 model_mommy/mommy.py
  2. +9 −0 runtests.py
View
@@ -5,9 +5,14 @@
from django.utils import importlib
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
-from django.db.models.loading import cache, get_model
+import django
+from django.db.models.loading import get_model
+if django.VERSION >= (1, 7):
+ from django.apps import apps
+else:
+ from django.db.models.loading import cache
from django.db.models.base import ModelBase
-from django.db.models import (\
+from django.db.models import (
CharField, EmailField, SlugField, TextField, URLField,
DateField, DateTimeField, TimeField,
AutoField, IntegerField, SmallIntegerField,
@@ -158,11 +163,15 @@ def get_model(self, name):
:param name String on the form 'applabel.modelname' or 'modelname'.
:return a model class.
'''
- if '.' in name:
- app_label, model_name = name.split('.')
- model = get_model(app_label, model_name)
- else:
- model = self.get_model_by_name(name)
+ try:
+ if '.' in name:
+ app_label, model_name = name.split('.')
+ model = get_model(app_label, model_name)
+ else:
+ model = self.get_model_by_name(name)
+ except LookupError: # Django 1.7.0a1 throws an exception
+ # Lower djangos just fail silently
+ model = None
if not model:
raise ModelNotFound("Could not find model '%s'." % name.title())
@@ -194,7 +203,12 @@ def _populate(self):
unique_models = {}
ambiguous_models = []
- for app_model in cache.app_models.values():
+ if django.VERSION >= (1, 7):
+ all_models = apps.all_models
+ else:
+ all_models = cache.app_models
+
+ for app_model in all_models.values():
for name, model in app_model.items():
if name not in unique_models:
unique_models[name] = model
View
@@ -4,6 +4,7 @@
import sys
from optparse import OptionParser
import warnings
+import django
def parse_args():
@@ -55,6 +56,12 @@ def get_runner(settings):
'''
from django.test.utils import get_runner
TestRunner = get_runner(settings)
+ if django.VERSION >= (1, 7):
+ # I suspect this will not be necessary in next release after 1.7.0a1:
+ # See https://code.djangoproject.com/ticket/21831
+ setattr(settings, 'INSTALLED_APPS',
+ ['django.contrib.auth']
+ + list(getattr(settings, 'INSTALLED_APPS')))
return TestRunner(verbosity=1, interactive=True, failfast=False)
@@ -64,6 +71,8 @@ def runtests(options=None, labels=None):
settings = configure_settings(options)
runner = get_runner(settings)
+ if django.VERSION >= (1, 7):
+ django.setup()
sys.exit(runner.run_tests(labels))

0 comments on commit 78208df

Please sign in to comment.