Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15903 -- Allowed not-installed models to still be referenced i…

…n related fields. Missed case from r16053.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16106 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c85b765288b2c36c948493a99706550eaf41a0b5 1 parent 2f9c52d
Carl Meyer authored April 27, 2011
12  django/db/models/loading.py
@@ -159,7 +159,8 @@ def get_app_errors(self):
159 159
         return self.app_errors
160 160
 
161 161
     def get_models(self, app_mod=None,
162  
-                   include_auto_created=False, include_deferred=False):
  162
+                   include_auto_created=False, include_deferred=False,
  163
+                   only_installed=True):
163 164
         """
164 165
         Given a module containing models, returns a list of the models.
165 166
         Otherwise returns a list of all installed models.
@@ -172,7 +173,7 @@ def get_models(self, app_mod=None,
172 173
         queries are *not* included in the list of models. However, if
173 174
         you specify include_deferred, they will be.
174 175
         """
175  
-        cache_key = (app_mod, include_auto_created, include_deferred)
  176
+        cache_key = (app_mod, include_auto_created, include_deferred, only_installed)
176 177
         try:
177 178
             return self._get_models_cache[cache_key]
178 179
         except KeyError:
@@ -185,8 +186,11 @@ def get_models(self, app_mod=None,
185 186
             else:
186 187
                 app_list = []
187 188
         else:
188  
-            app_list = [self.app_models.get(app_label, SortedDict())
189  
-                        for app_label in self.app_labels.iterkeys()]
  189
+            if only_installed:
  190
+                app_list = [self.app_models.get(app_label, SortedDict())
  191
+                            for app_label in self.app_labels.iterkeys()]
  192
+            else:
  193
+                app_list = self.app_models.itervalues()
190 194
         model_list = []
191 195
         for app in app_list:
192 196
             model_list.extend(
2  django/db/models/options.py
@@ -383,7 +383,7 @@ def _fill_related_objects_cache(self):
383 383
                     cache[obj] = parent
384 384
                 else:
385 385
                     cache[obj] = model
386  
-        for klass in get_models(include_auto_created=True):
  386
+        for klass in get_models(include_auto_created=True, only_installed=False):
387 387
             for f in klass._meta.local_fields:
388 388
                 if f.rel and not isinstance(f.rel.to, str) and self == f.rel.to._meta:
389 389
                     cache[RelatedObject(f.rel.to, klass, f)] = None
4  tests/regressiontests/app_loading/not_installed/models.py
@@ -3,3 +3,7 @@
3 3
 
4 4
 class NotInstalledModel(models.Model):
5 5
     pass
  6
+
  7
+
  8
+class RelatedModel(models.Model):
  9
+    not_installed = models.ForeignKey(NotInstalledModel)
25  tests/regressiontests/app_loading/tests.py
@@ -85,8 +85,8 @@ def test_egg5(self):
85 85
 
86 86
 class GetModelsTest(TestCase):
87 87
     def setUp(self):
88  
-        import not_installed.models
89  
-        self.not_installed_module = not_installed.models
  88
+        from .not_installed import models
  89
+        self.not_installed_module = models
90 90
 
91 91
 
92 92
     def test_get_model_only_returns_installed_models(self):
@@ -94,6 +94,13 @@ def test_get_model_only_returns_installed_models(self):
94 94
             get_model("not_installed", "NotInstalledModel"), None)
95 95
 
96 96
 
  97
+    def test_get_model_with_not_installed(self):
  98
+        self.assertEqual(
  99
+            get_model(
  100
+                "not_installed", "NotInstalledModel", only_installed=False),
  101
+            self.not_installed_module.NotInstalledModel)
  102
+
  103
+
97 104
     def test_get_models_only_returns_installed_models(self):
98 105
         self.assertFalse(
99 106
             "NotInstalledModel" in
@@ -102,3 +109,17 @@ def test_get_models_only_returns_installed_models(self):
102 109
 
103 110
     def test_get_models_with_app_label_only_returns_installed_models(self):
104 111
         self.assertEqual(get_models(self.not_installed_module), [])
  112
+
  113
+
  114
+    def test_get_models_with_not_installed(self):
  115
+        self.assertTrue(
  116
+            "NotInstalledModel" in [
  117
+                m.__name__ for m in get_models(only_installed=False)])
  118
+
  119
+
  120
+class NotInstalledModelsTest(TestCase):
  121
+    def test_related_not_installed_model(self):
  122
+        from .not_installed.models import NotInstalledModel
  123
+        self.assertEqual(
  124
+            set(NotInstalledModel._meta.get_all_field_names()),
  125
+            set(["id", "relatedmodel"]))

0 notes on commit c85b765

Please sign in to comment.
Something went wrong with that request. Please try again.