Code

Ticket #11936: fix_r11901.diff

File fix_r11901.diff, 2.3 KB (added by clamothe, 4 years ago)

Fix and regression test against r11901

Line 
1diff --git a/django/db/models/loading.py b/django/db/models/loading.py
2index f86b691..bc62aa0 100644
3--- a/django/db/models/loading.py
4+++ b/django/db/models/loading.py
5@@ -132,7 +132,7 @@ class AppCache(object):
6         self._populate()
7         return self.app_errors
8 
9-    def get_models(self, app_mod=None, include_auto_created=False):
10+    def get_models(self, app_mod=None, include_auto_created=False, include_deferred=False):
11         """
12         Given a module containing models, returns a list of the models.
13         Otherwise returns a list of all installed models.
14@@ -141,7 +141,7 @@ class AppCache(object):
15         explicit intermediate table) are not included. However, if you
16         specify include_auto_created=True, they will be.
17         """
18-        cache_key = (app_mod, include_auto_created)
19+        cache_key = (app_mod, include_auto_created, include_deferred)
20         try:
21             return self._get_models_cache[cache_key]
22         except KeyError:
23@@ -155,6 +155,8 @@ class AppCache(object):
24                 model_list.extend(app_entry.values())
25         if not include_auto_created:
26             model_list = filter(lambda o: not o._meta.auto_created, model_list)
27+        if not include_deferred:
28+            model_list = filter(lambda o: not o._deferred, model_list)
29         self._get_models_cache[cache_key] = model_list
30         return model_list
31 
32diff --git a/tests/regressiontests/defer_regress/models.py b/tests/regressiontests/defer_regress/models.py
33index a1cd197..23d855b 100644
34--- a/tests/regressiontests/defer_regress/models.py
35+++ b/tests/regressiontests/defer_regress/models.py
36@@ -132,6 +132,11 @@ False
37 >>> i2._deferred # Item must still be non-deferred
38 False
39 
40+# Regression for #11936 - loading.get_models should not return deferred models by default.
41+>>> from django.db.models.loading import get_models
42+>>> get_models(models.get_app('defer_regress'))
43+[<class 'regressiontests.defer_regress.models.Item'>, <class 'regressiontests.defer_regress.models.RelatedItem'>, <class 'regressiontests.defer_regress.models.Child'>, <class 'regressiontests.defer_regress.models.Leaf'>]
44+
45 # Finally, we need to flush the app cache for the defer module.
46 # Using only/defer creates some artifical entries in the app cache
47 # that messes up later tests. Purge all entries, just to be sure.