Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Commit

Permalink
Updates for Django 1.7: locate models in the new app registry
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfe committed Feb 8, 2014
1 parent e6ad8ea commit 78208df
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
30 changes: 22 additions & 8 deletions model_mommy/mommy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
from optparse import OptionParser
import warnings
import django


def parse_args():
Expand Down Expand Up @@ -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)


Expand All @@ -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))


Expand Down

0 comments on commit 78208df

Please sign in to comment.