diff options
| author | Simon Charette <charette.s@gmail.com> | 2015-12-18 14:49:23 -0500 |
|---|---|---|
| committer | Simon Charette <charette.s@gmail.com> | 2016-02-26 16:22:17 -0500 |
| commit | ba6f83ec95dbd265ae3d493c8d6375f36052d12e (patch) | |
| tree | 1912ca6010e8e771719d42c0527b391c3d304d07 /django | |
| parent | cd46947ddb6719c819e75464d6aa0a10a6c10fad (diff) | |
[1.9.x] Fixed #26286 -- Prevented content type managers from sharing their cache.
This should prevent managers methods from returning content type instances
registered to foreign apps now that these managers are also attached to models
created during migration phases.
Thanks Tim for the review.
Refs #23822.
Backport of 3938b3ccaa85f1c366909a4839696007726a09da from master
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/contenttypes/models.py | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py index 27a5388a70..8d459f65a2 100644 --- a/django/contrib/contenttypes/models.py +++ b/django/contrib/contenttypes/models.py @@ -13,13 +13,15 @@ from django.utils.translation import ugettext_lazy as _ class ContentTypeManager(models.Manager): use_in_migrations = True - # Cache to avoid re-looking up ContentType objects all over the place. - # This cache is shared by all the get_for_* methods. - _cache = {} + def __init__(self, *args, **kwargs): + super(ContentTypeManager, self).__init__(*args, **kwargs) + # Cache shared by all the get_for_* methods to speed up + # ContentType retrieval. + self._cache = {} def get_by_natural_key(self, app_label, model): try: - ct = self.__class__._cache[self.db][(app_label, model)] + ct = self._cache[self.db][(app_label, model)] except KeyError: ct = self.get(app_label=app_label, model=model) self._add_to_cache(self.db, ct) @@ -34,7 +36,7 @@ class ContentTypeManager(models.Manager): def _get_from_cache(self, opts): key = (opts.app_label, opts.model_name) - return self.__class__._cache[self.db][key] + return self._cache[self.db][key] def create(self, **kwargs): if 'name' in kwargs: @@ -129,7 +131,7 @@ class ContentTypeManager(models.Manager): (though ContentTypes are obviously not created on-the-fly by get_by_id). """ try: - ct = self.__class__._cache[self.db][id] + ct = self._cache[self.db][id] except KeyError: # This could raise a DoesNotExist; that's correct behavior and will # make sure that only correct ctypes get stored in the cache dict. @@ -144,15 +146,15 @@ class ContentTypeManager(models.Manager): django.contrib.contenttypes.management.update_contenttypes for where this gets called). """ - self.__class__._cache.clear() + self._cache.clear() def _add_to_cache(self, using, ct): """Insert a ContentType into the cache.""" # Note it's possible for ContentType objects to be stale; model_class() will return None. # Hence, there is no reliance on model._meta.app_label here, just using the model fields instead. key = (ct.app_label, ct.model) - self.__class__._cache.setdefault(using, {})[key] = ct - self.__class__._cache.setdefault(using, {})[ct.id] = ct + self._cache.setdefault(using, {})[key] = ct + self._cache.setdefault(using, {})[ct.id] = ct @python_2_unicode_compatible |
