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.
This commit is contained in:
Aymeric Augustin 2013-12-28 14:24:14 +01:00
parent bbdf01e00a
commit 81354b82bf
3 changed files with 7 additions and 16 deletions

View File

@ -157,7 +157,7 @@ class ContentType(models.Model):
def model_class(self): def model_class(self):
"Returns the Python model class for this type of content." "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): def get_object_for_this_type(self, **kwargs):
""" """

View File

@ -1,5 +1,3 @@
from collections import defaultdict
from django.apps import apps from django.apps import apps
from django.dispatch import Signal from django.dispatch import Signal
from django.utils import six from django.utils import six
@ -16,7 +14,7 @@ class ModelSignal(Signal):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(ModelSignal, self).__init__(*args, **kwargs) super(ModelSignal, self).__init__(*args, **kwargs)
self.unresolved_references = defaultdict(list) self.unresolved_references = {}
class_prepared.connect(self._resolve_references) class_prepared.connect(self._resolve_references)
def _resolve_references(self, sender, **kwargs): def _resolve_references(self, sender, **kwargs):
@ -35,18 +33,17 @@ class ModelSignal(Signal):
def connect(self, receiver, sender=None, weak=True, dispatch_uid=None): def connect(self, receiver, sender=None, weak=True, dispatch_uid=None):
if isinstance(sender, six.string_types): if isinstance(sender, six.string_types):
try: try:
app_label, object_name = sender.split('.') app_label, model_name = sender.split('.')
except ValueError: except ValueError:
raise ValueError( raise ValueError(
"Specified sender must either be a model or a " "Specified sender must either be a model or a "
"model name of the 'app_label.ModelName' form." "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: if sender is None:
reference = (app_label, object_name) ref = (app_label, model_name)
self.unresolved_references[reference].append( refs = self.unresolved_references.setdefault(ref, [])
(receiver, weak, dispatch_uid) refs.append((receiver, weak, dispatch_uid))
)
return return
super(ModelSignal, self).connect( super(ModelSignal, self).connect(
receiver, sender=sender, weak=weak, dispatch_uid=dispatch_uid receiver, sender=sender, weak=weak, dispatch_uid=dispatch_uid

View File

@ -75,12 +75,6 @@ class GetModelsTest(TestCase):
self.assertEqual( self.assertEqual(
apps.get_model("not_installed", "NotInstalledModel"), None) 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): def test_get_models_only_returns_installed_models(self):
self.assertNotIn( self.assertNotIn(
"NotInstalledModel", "NotInstalledModel",