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:
parent
e4dc8be3a9
commit
4eecb92fc4
@ -45,7 +45,7 @@ class GenericForeignKey(object):
|
|||||||
kwargs[self.ct_field] = self.get_content_type(obj=value)
|
kwargs[self.ct_field] = self.get_content_type(obj=value)
|
||||||
kwargs[self.fk_field] = value._get_pk_val()
|
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
|
# Convenience function using get_model avoids a circular import when
|
||||||
# using this model
|
# using this model
|
||||||
ContentType = get_model("contenttypes", "contenttype")
|
ContentType = get_model("contenttypes", "contenttype")
|
||||||
|
@ -8,52 +8,55 @@ class ContentTypeManager(models.Manager):
|
|||||||
# This cache is shared by all the get_for_* methods.
|
# This cache is shared by all the get_for_* methods.
|
||||||
_cache = {}
|
_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:
|
try:
|
||||||
ct = self.__class__._cache[using][(app_label, model)]
|
ct = self.__class__._cache[db][(app_label, model)]
|
||||||
except KeyError:
|
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
|
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
|
Returns the ContentType object for a given model, creating the
|
||||||
ContentType if necessary. Lookups are cached so that subsequent lookups
|
ContentType if necessary. Lookups are cached so that subsequent lookups
|
||||||
for the same model don't hit the database.
|
for the same model don't hit the database.
|
||||||
"""
|
"""
|
||||||
|
db = using or DEFAULT_DB_ALIAS
|
||||||
opts = model._meta
|
opts = model._meta
|
||||||
while opts.proxy:
|
while opts.proxy:
|
||||||
model = opts.proxy_for_model
|
model = opts.proxy_for_model
|
||||||
opts = model._meta
|
opts = model._meta
|
||||||
key = (opts.app_label, opts.object_name.lower())
|
key = (opts.app_label, opts.object_name.lower())
|
||||||
try:
|
try:
|
||||||
ct = self.__class__._cache[using][key]
|
ct = self.__class__._cache[db][key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# Load or create the ContentType entry. The smart_unicode() is
|
# Load or create the ContentType entry. The smart_unicode() is
|
||||||
# needed around opts.verbose_name_raw because name_raw might be a
|
# needed around opts.verbose_name_raw because name_raw might be a
|
||||||
# django.utils.functional.__proxy__ object.
|
# 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,
|
app_label = opts.app_label,
|
||||||
model = opts.object_name.lower(),
|
model = opts.object_name.lower(),
|
||||||
defaults = {'name': smart_unicode(opts.verbose_name_raw)},
|
defaults = {'name': smart_unicode(opts.verbose_name_raw)},
|
||||||
)
|
)
|
||||||
self._add_to_cache(using, ct)
|
self._add_to_cache(db, ct)
|
||||||
|
|
||||||
return 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
|
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).
|
(though ContentTypes are obviously not created on-the-fly by get_by_id).
|
||||||
"""
|
"""
|
||||||
|
db = using or DEFAULT_DB_ALIAS
|
||||||
try:
|
try:
|
||||||
ct = self.__class__._cache[using][id]
|
ct = self.__class__._cache[db][id]
|
||||||
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# This could raise a DoesNotExist; that's correct behavior and will
|
# This could raise a DoesNotExist; that's correct behavior and will
|
||||||
# make sure that only correct ctypes get stored in the cache dict.
|
# make sure that only correct ctypes get stored in the cache dict.
|
||||||
ct = self.using(using).get(pk=id)
|
ct = self.using(db).get(pk=id)
|
||||||
self._add_to_cache(using, ct)
|
self._add_to_cache(db, ct)
|
||||||
return ct
|
return ct
|
||||||
|
|
||||||
def clear_cache(self):
|
def clear_cache(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user