From 81354b82bf647fd100e836dae2639e9c8d76c5eb Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 28 Dec 2013 14:24:14 +0100 Subject: [PATCH] Stopped calling apps.get_model with only_installed=False. ContentTypes are only created for installed applications, and I could make a case for not returning a model that isn't installed any more. The check for stale ContentTypes in update_contenttypes doesn't use model_class. ModelSignal actually needs get_registered_model since the lookup happens at import time. I took this opportunity to perform a small refactoring. --- django/contrib/contenttypes/models.py | 2 +- django/db/models/signals.py | 15 ++++++--------- tests/app_loading/tests.py | 6 ------ 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py index 6df1087cae..6b80b9fe7e 100644 --- a/django/contrib/contenttypes/models.py +++ b/django/contrib/contenttypes/models.py @@ -157,7 +157,7 @@ class ContentType(models.Model): def model_class(self): "Returns the Python model class for this type of content." - return apps.get_model(self.app_label, self.model, only_installed=False) + return apps.get_model(self.app_label, self.model) def get_object_for_this_type(self, **kwargs): """ diff --git a/django/db/models/signals.py b/django/db/models/signals.py index 10da3e22a5..97352460ee 100644 --- a/django/db/models/signals.py +++ b/django/db/models/signals.py @@ -1,5 +1,3 @@ -from collections import defaultdict - from django.apps import apps from django.dispatch import Signal from django.utils import six @@ -16,7 +14,7 @@ class ModelSignal(Signal): def __init__(self, *args, **kwargs): super(ModelSignal, self).__init__(*args, **kwargs) - self.unresolved_references = defaultdict(list) + self.unresolved_references = {} class_prepared.connect(self._resolve_references) def _resolve_references(self, sender, **kwargs): @@ -35,18 +33,17 @@ class ModelSignal(Signal): def connect(self, receiver, sender=None, weak=True, dispatch_uid=None): if isinstance(sender, six.string_types): try: - app_label, object_name = sender.split('.') + app_label, model_name = sender.split('.') except ValueError: raise ValueError( "Specified sender must either be a model or a " "model name of the 'app_label.ModelName' form." ) - sender = apps.get_model(app_label, object_name, only_installed=False) + sender = apps.get_registered_model(app_label, model_name) if sender is None: - reference = (app_label, object_name) - self.unresolved_references[reference].append( - (receiver, weak, dispatch_uid) - ) + ref = (app_label, model_name) + refs = self.unresolved_references.setdefault(ref, []) + refs.append((receiver, weak, dispatch_uid)) return super(ModelSignal, self).connect( receiver, sender=sender, weak=weak, dispatch_uid=dispatch_uid diff --git a/tests/app_loading/tests.py b/tests/app_loading/tests.py index 19a1d03d74..d61205330a 100644 --- a/tests/app_loading/tests.py +++ b/tests/app_loading/tests.py @@ -75,12 +75,6 @@ class GetModelsTest(TestCase): self.assertEqual( apps.get_model("not_installed", "NotInstalledModel"), None) - def test_get_model_with_not_installed(self): - self.assertEqual( - apps.get_model( - "not_installed", "NotInstalledModel", only_installed=False), - self.not_installed_module.NotInstalledModel) - def test_get_models_only_returns_installed_models(self): self.assertNotIn( "NotInstalledModel",