diff options
| author | Antonis Christofides <antonis@wikical.com> | 2013-11-24 18:12:46 +0100 |
|---|---|---|
| committer | Loic Bistuer <loic.bistuer@gmail.com> | 2014-05-29 23:01:55 +0700 |
| commit | 62f9508ade5233fc7bf8af5b3afcd2f5b57d8288 (patch) | |
| tree | a897162c21381e21f9554210b664495db7cf6cd0 /django | |
| parent | d240b29c086f434c87c2a5be7af539ceb8c0f55f (diff) | |
Fixed #20401 -- ContentTypeManager.get_for_model reads from db_for_read.
Thanks Simon Charette and Tim Graham for the reviews.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/contenttypes/models.py | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py index 0dc048cfa2..634576f4d0 100644 --- a/django/contrib/contenttypes/models.py +++ b/django/contrib/contenttypes/models.py @@ -38,18 +38,27 @@ class ContentTypeManager(models.Manager): """ opts = self._get_opts(model, for_concrete_model) try: - ct = self._get_from_cache(opts) + return self._get_from_cache(opts) except KeyError: - # Load or create the ContentType entry. The smart_text() is - # needed around opts.verbose_name_raw because name_raw might be a - # django.utils.functional.__proxy__ object. + pass + + # The ContentType entry was not found in the cache, therefore we + # proceed to load or create it. + try: + # We start with get() and not get_or_create() in order to use + # the db_for_read (see #20401). + ct = self.get(app_label=opts.app_label, model=opts.model_name) + except self.model.DoesNotExist: + # Not found in the database; we proceed to create it. This time we + # use get_or_create to take care of any race conditions. + # The smart_text() is needed around opts.verbose_name_raw because + # name_raw might be a django.utils.functional.__proxy__ object. ct, created = self.get_or_create( app_label=opts.app_label, model=opts.model_name, defaults={'name': smart_text(opts.verbose_name_raw)}, ) - self._add_to_cache(self.db, ct) - + self._add_to_cache(self.db, ct) return ct def get_for_models(self, *models, **kwargs): |
