Fixed #15903 -- Allowed not-installed models to still be referenced in related fields. Missed case from r16053.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16106 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Carl Meyer 2011-04-27 15:46:43 +00:00
parent 2f9c52dc90
commit c85b765288
4 changed files with 36 additions and 7 deletions

View File

@ -159,7 +159,8 @@ class AppCache(object):
return self.app_errors return self.app_errors
def get_models(self, app_mod=None, def get_models(self, app_mod=None,
include_auto_created=False, include_deferred=False): include_auto_created=False, include_deferred=False,
only_installed=True):
""" """
Given a module containing models, returns a list of the models. Given a module containing models, returns a list of the models.
Otherwise returns a list of all installed models. Otherwise returns a list of all installed models.
@ -172,7 +173,7 @@ class AppCache(object):
queries are *not* included in the list of models. However, if queries are *not* included in the list of models. However, if
you specify include_deferred, they will be. you specify include_deferred, they will be.
""" """
cache_key = (app_mod, include_auto_created, include_deferred) cache_key = (app_mod, include_auto_created, include_deferred, only_installed)
try: try:
return self._get_models_cache[cache_key] return self._get_models_cache[cache_key]
except KeyError: except KeyError:
@ -185,8 +186,11 @@ class AppCache(object):
else: else:
app_list = [] app_list = []
else: else:
if only_installed:
app_list = [self.app_models.get(app_label, SortedDict()) app_list = [self.app_models.get(app_label, SortedDict())
for app_label in self.app_labels.iterkeys()] for app_label in self.app_labels.iterkeys()]
else:
app_list = self.app_models.itervalues()
model_list = [] model_list = []
for app in app_list: for app in app_list:
model_list.extend( model_list.extend(

View File

@ -383,7 +383,7 @@ class Options(object):
cache[obj] = parent cache[obj] = parent
else: else:
cache[obj] = model cache[obj] = model
for klass in get_models(include_auto_created=True): for klass in get_models(include_auto_created=True, only_installed=False):
for f in klass._meta.local_fields: for f in klass._meta.local_fields:
if f.rel and not isinstance(f.rel.to, str) and self == f.rel.to._meta: if f.rel and not isinstance(f.rel.to, str) and self == f.rel.to._meta:
cache[RelatedObject(f.rel.to, klass, f)] = None cache[RelatedObject(f.rel.to, klass, f)] = None

View File

@ -3,3 +3,7 @@ from django.db import models
class NotInstalledModel(models.Model): class NotInstalledModel(models.Model):
pass pass
class RelatedModel(models.Model):
not_installed = models.ForeignKey(NotInstalledModel)

View File

@ -85,8 +85,8 @@ class EggLoadingTest(TestCase):
class GetModelsTest(TestCase): class GetModelsTest(TestCase):
def setUp(self): def setUp(self):
import not_installed.models from .not_installed import models
self.not_installed_module = not_installed.models self.not_installed_module = models
def test_get_model_only_returns_installed_models(self): def test_get_model_only_returns_installed_models(self):
@ -94,6 +94,13 @@ class GetModelsTest(TestCase):
get_model("not_installed", "NotInstalledModel"), None) get_model("not_installed", "NotInstalledModel"), None)
def test_get_model_with_not_installed(self):
self.assertEqual(
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.assertFalse( self.assertFalse(
"NotInstalledModel" in "NotInstalledModel" in
@ -102,3 +109,17 @@ class GetModelsTest(TestCase):
def test_get_models_with_app_label_only_returns_installed_models(self): def test_get_models_with_app_label_only_returns_installed_models(self):
self.assertEqual(get_models(self.not_installed_module), []) self.assertEqual(get_models(self.not_installed_module), [])
def test_get_models_with_not_installed(self):
self.assertTrue(
"NotInstalledModel" in [
m.__name__ for m in get_models(only_installed=False)])
class NotInstalledModelsTest(TestCase):
def test_related_not_installed_model(self):
from .not_installed.models import NotInstalledModel
self.assertEqual(
set(NotInstalledModel._meta.get_all_field_names()),
set(["id", "relatedmodel"]))