mirror of
https://github.com/django/django.git
synced 2024-11-29 06:03:25 +01:00
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:
parent
bbdf01e00a
commit
81354b82bf
@ -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):
|
||||
"""
|
||||
|
@ -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
|
||||
|
@ -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",
|
||||
|
Loading…
Reference in New Issue
Block a user