title: Routing and middleware for multiple databases in Django
body: <p>I have a problem correctly setting my middleware and routers to support multiple databases, one for each language (I decided to keep them separate).</p>
  
  <p>I tried to use <a href="https://stackoverflow.com/questions/16215122/url-and-database-routing-based-on-language">this solution</a>, but for now I didn't get much use of it.</p>
  
  <p>In my <em>settings.py</em> databases and middleware are defined as follows:</p>
  
  <pre class="lang-py prettyprint-override"><code>DATABASES = {
      'default': {},
      'ru': {
          'ENGINE': 'django.db.backends.sqlite3',
          'NAME': 'db_ru.sqlite3',
      },
      'en': {
          'ENGINE': 'django.db.backends.sqlite3',
          'NAME': 'db_en.sqlite3',
      }
  }
  
  DATABASE_ROUTERS = ['me.middleware.database_routing.DatabaseRouter']
  
  MIDDLEWARE = [
      'me.middleware.database_routing.RouterMiddleware',
      'django.middleware.security.SecurityMiddleware',
      'whitenoise.middleware.WhiteNoiseMiddleware',
      'django.contrib.sessions.middleware.SessionMiddleware',
      'django.middleware.locale.LocaleMiddleware',
      'django.middleware.common.CommonMiddleware',
      'django.middleware.csrf.CsrfViewMiddleware',
      'django.contrib.auth.middleware.AuthenticationMiddleware',
      'django.contrib.messages.middleware.MessageMiddleware',
      'django.middleware.clickjacking.XFrameOptionsMiddleware',
  ]
  </code></pre>
  
  <p><em>database_routing.py</em> used in <code>'me.middleware.database_routing'</code> fully corresponds to <a href="https://stackoverflow.com/questions/16215122/url-and-database-routing-based-on-language">this middleware</a>.</p>
  
  <p>I get an error when starting the server: <em>RouterMiddleware() takes no arguments</em>. I believe there's some shortcoming concerning the middleware code. Also I cannot migrate to my databases, I get <code>ValueError: Cannot assign "&lt;ContentType: ContentType object (1)&gt;": the current database router prevents this relation.</code></p>
  
  <p>Maybe, there is some other solution?</p>