diff --git a/django/contrib/contenttypes/generic.py b/django/contrib/contenttypes/generic.py index 9cfc3a8cbf..0ed8ea5e03 100644 --- a/django/contrib/contenttypes/generic.py +++ b/django/contrib/contenttypes/generic.py @@ -45,7 +45,7 @@ class GenericForeignKey(object): kwargs[self.ct_field] = self.get_content_type(obj=value) kwargs[self.fk_field] = value._get_pk_val() - def get_content_type(self, obj=None, id=None, using=DEFAULT_DB_ALIAS): + def get_content_type(self, obj=None, id=None, using=None): # Convenience function using get_model avoids a circular import when # using this model ContentType = get_model("contenttypes", "contenttype") diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py index f95683d432..0e572742e1 100644 --- a/django/contrib/contenttypes/models.py +++ b/django/contrib/contenttypes/models.py @@ -8,52 +8,55 @@ class ContentTypeManager(models.Manager): # This cache is shared by all the get_for_* methods. _cache = {} - def get_by_natural_key(self, app_label, model, using=DEFAULT_DB_ALIAS): + def get_by_natural_key(self, app_label, model, using=None): + db = using or DEFAULT_DB_ALIAS try: - ct = self.__class__._cache[using][(app_label, model)] + ct = self.__class__._cache[db][(app_label, model)] except KeyError: - ct = self.using(using).get(app_label=app_label, model=model) + ct = self.using(db).get(app_label=app_label, model=model) return ct - def get_for_model(self, model, using=DEFAULT_DB_ALIAS): + def get_for_model(self, model, using=None): """ Returns the ContentType object for a given model, creating the ContentType if necessary. Lookups are cached so that subsequent lookups for the same model don't hit the database. """ + db = using or DEFAULT_DB_ALIAS opts = model._meta while opts.proxy: model = opts.proxy_for_model opts = model._meta key = (opts.app_label, opts.object_name.lower()) try: - ct = self.__class__._cache[using][key] + ct = self.__class__._cache[db][key] except KeyError: # Load or create the ContentType entry. The smart_unicode() is # needed around opts.verbose_name_raw because name_raw might be a # django.utils.functional.__proxy__ object. - ct, created = self.using(using).get_or_create( + ct, created = self.using(db).get_or_create( app_label = opts.app_label, model = opts.object_name.lower(), defaults = {'name': smart_unicode(opts.verbose_name_raw)}, ) - self._add_to_cache(using, ct) + self._add_to_cache(db, ct) return ct - def get_for_id(self, id, using=DEFAULT_DB_ALIAS): + def get_for_id(self, id, using=None): """ Lookup a ContentType by ID. Uses the same shared cache as get_for_model (though ContentTypes are obviously not created on-the-fly by get_by_id). """ + db = using or DEFAULT_DB_ALIAS try: - ct = self.__class__._cache[using][id] + ct = self.__class__._cache[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. - ct = self.using(using).get(pk=id) - self._add_to_cache(using, ct) + ct = self.using(db).get(pk=id) + self._add_to_cache(db, ct) return ct def clear_cache(self):