﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
20401	get_for_model queries the wrong database	Antonis Christofides	Antonis Christofides	"The {{{django.contrib.contenttypes.models.ContentTypeManager.get_for_model}}} method, if it doesn't find the requested content type in the cache, uses {{{get_or_create}}} to get it from the database. In multi-database environments, {{{get_or_create}}} always queries the db_for_write and therefore I think it is inappropriate for this case, where there is an overwhelming probability that the object requested already exists and therefore no write will occur.

Here is a copy of the method for reference:

{{{#!python
    def get_for_model(self, model, for_concrete_model=True):
        """"""
        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.
        """"""
        opts = self._get_opts(model, for_concrete_model)
        try:
            ct = 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.
            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)

        return ct
}}}"	Bug	closed	contrib.contenttypes	dev	Normal	fixed			Accepted	1	0	0	1	0	0
