Skip to content

Commit

Permalink
Fixed #11936 -- Removed deferred models from the list returned by the…
Browse files Browse the repository at this point in the history
… app_cache. Thanks to ryszard for the report, and clamothe for the initial patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@11938 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Dec 22, 2009
1 parent 15f42cc commit 3d00992
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
23 changes: 15 additions & 8 deletions django/db/models/loading.py
Expand Up @@ -132,29 +132,36 @@ def get_app_errors(self):
self._populate() self._populate()
return self.app_errors return self.app_errors


def get_models(self, app_mod=None, include_auto_created=False): def get_models(self, app_mod=None, include_auto_created=False, include_deferred=False):
""" """
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.
By default, auto-created models (i.e., m2m models without an By default, auto-created models (i.e., m2m models without an
explicit intermediate table) are not included. However, if you explicit intermediate table) are not included. However, if you
specify include_auto_created=True, they will be. specify include_auto_created=True, they will be.
By default, models created to satisfy deferred attribute
queries are *not* included in the list of models. However, if
you specify include_deferred, they will be.
""" """
cache_key = (app_mod, include_auto_created) cache_key = (app_mod, include_auto_created, include_deferred)
try: try:
return self._get_models_cache[cache_key] return self._get_models_cache[cache_key]
except KeyError: except KeyError:
pass pass
self._populate() self._populate()
if app_mod: if app_mod:
model_list = self.app_models.get(app_mod.__name__.split('.')[-2], SortedDict()).values() app_list = [self.app_models.get(app_mod.__name__.split('.')[-2], SortedDict())]
else: else:
model_list = [] app_list = self.app_models.itervalues()
for app_entry in self.app_models.itervalues(): model_list = []
model_list.extend(app_entry.values()) for app in app_list:
if not include_auto_created: model_list.extend(
model_list = filter(lambda o: not o._meta.auto_created, model_list) model for model in app.values()
if ((not model._deferred or include_deferred)
and (not model._meta.auto_created or include_auto_created))
)
self._get_models_cache[cache_key] = model_list self._get_models_cache[cache_key] = model_list
return model_list return model_list


Expand Down
6 changes: 0 additions & 6 deletions tests/modeltests/defer/models.py
Expand Up @@ -183,10 +183,4 @@ def count_delayed_fields(obj, debug=False):
>>> obj.name = "bb" >>> obj.name = "bb"
>>> obj.save() >>> obj.save()
# Finally, we need to flush the app cache for the defer module.
# Using only/defer creates some artifical entries in the app cache
# that messes up later tests. Purge all entries, just to be sure.
>>> from django.db.models.loading import cache
>>> cache.app_models['defer'] = {}
"""} """}
13 changes: 8 additions & 5 deletions tests/regressiontests/defer_regress/models.py
Expand Up @@ -132,11 +132,14 @@ def __unicode__(self):
>>> i2._deferred # Item must still be non-deferred >>> i2._deferred # Item must still be non-deferred
False False
# Finally, we need to flush the app cache for the defer module. # Regression for #11936 - loading.get_models should not return deferred models by default.
# Using only/defer creates some artifical entries in the app cache >>> from django.db.models.loading import get_models
# that messes up later tests. Purge all entries, just to be sure. >>> sorted(get_models(models.get_app('defer_regress')), key=lambda obj: obj.__class__.__name__)
>>> from django.db.models.loading import cache [<class 'regressiontests.defer_regress.models.Item'>, <class 'regressiontests.defer_regress.models.RelatedItem'>, <class 'regressiontests.defer_regress.models.Child'>, <class 'regressiontests.defer_regress.models.Leaf'>]
>>> cache.app_models['defer_regress'] = {}
>>> sorted(get_models(models.get_app('defer_regress'), include_deferred=True), key=lambda obj: obj.__class__.__name__)
[<class 'regressiontests.defer_regress.models.Item'>, <class 'regressiontests.defer_regress.models.RelatedItem'>, <class 'regressiontests.defer_regress.models.Child'>, <class 'regressiontests.defer_regress.models.Leaf'>, <class 'regressiontests.defer_regress.models.Item_Deferred_text_value'>, <class 'regressiontests.defer_regress.models.Item_Deferred_name_other_value_text'>, <class 'regressiontests.defer_regress.models.RelatedItem_Deferred_item_id'>, <class 'regressiontests.defer_regress.models.Leaf_Deferred_second_child_value'>, <class 'regressiontests.defer_regress.models.Leaf_Deferred_name_value'>, <class 'regressiontests.defer_regress.models.Item_Deferred_name'>, <class 'regressiontests.defer_regress.models.Item_Deferred_other_value_text_value'>, <class 'regressiontests.defer_regress.models.Leaf_Deferred_value'>]
""" """
} }

0 comments on commit 3d00992

Please sign in to comment.