diff -r cf42f6bf945b django/db/models/loading.py
a
|
b
|
|
12 | 12 | __all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models', |
13 | 13 | 'load_app', 'app_cache_ready') |
14 | 14 | |
| 15 | def filter_deferred_models(models): |
| 16 | """ |
| 17 | Takes a list of models and returns the models that are not deferred. |
| 18 | |
| 19 | Deferred models are temporary models that are created to represent the |
| 20 | results of queries that are made using the ``only`` and ``defer`` |
| 21 | ``QuerySet`` methods. |
| 22 | """ |
| 23 | return [model for model in models if not model._deferred] |
| 24 | |
15 | 25 | class AppCache(object): |
16 | 26 | """ |
17 | 27 | A cache that stores installed applications and their models. Used to |
… |
… |
|
135 | 145 | """ |
136 | 146 | Given a module containing models, returns a list of the models. |
137 | 147 | Otherwise returns a list of all installed models. |
| 148 | |
| 149 | Temporary models that were created for representing the results of |
| 150 | deferred-field querysets are not included. |
138 | 151 | """ |
139 | 152 | self._populate() |
140 | 153 | if app_mod: |
141 | | return self.app_models.get(app_mod.__name__.split('.')[-2], SortedDict()).values() |
| 154 | model_list = self.app_models.get(app_mod.__name__.split('.')[-2], SortedDict()).values() |
142 | 155 | else: |
143 | 156 | model_list = [] |
144 | 157 | for app_entry in self.app_models.itervalues(): |
145 | 158 | model_list.extend(app_entry.values()) |
146 | | return model_list |
| 159 | |
| 160 | return filter_deferred_models(model_list) |
147 | 161 | |
148 | 162 | def get_model(self, app_label, model_name, seed_cache=True): |
149 | 163 | """ |
diff -r cf42f6bf945b tests/modeltests/defer/models.py
a
|
b
|
|
183 | 183 | >>> obj.name = "bb" |
184 | 184 | >>> obj.save() |
185 | 185 | |
| 186 | # Regression for #11936 - loading.get_models does not return deferred models |
| 187 | >>> from django.db.models.loading import get_models |
| 188 | >>> get_models(models.get_app('defer')) |
| 189 | [<class 'modeltests.defer.models.Secondary'>, <class 'modeltests.defer.models.Primary'>, <class 'modeltests.defer.models.Child'>, <class 'modeltests.defer.models.BigChild'>] |
| 190 | |
186 | 191 | # Finally, we need to flush the app cache for the defer module. |
187 | 192 | # Using only/defer creates some artifical entries in the app cache |
188 | 193 | # that messes up later tests. Purge all entries, just to be sure. |