1
0
mirror of https://github.com/django/django.git synced 2025-07-04 17:59:13 +00:00

[soc2009/multidb] More cleanups of using= arguments. Patch from Russell Keith-Magee.

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@11895 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2009-12-17 16:13:25 +00:00
parent e4dc8be3a9
commit 4eecb92fc4
2 changed files with 15 additions and 12 deletions

View File

@ -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")

View File

@ -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):