title: Use mongodb as second database for django app
body: <p>I'm trying to setup two databases for my django app. The first is used for users and the second for any type of user content. The app is dockerized via docker compose.</p>
  
  <p>Here's the docker-compose.yaml file</p>
  
  <pre><code>version: '3'
  
  services:
    postgres:
      image: postgres:latest
      environment:
        - POSTGRES_PASSWORD=test
      ports:
        - "5432:5432"
      restart: always
    mongodb:
      image: mongo:latest
      command: ["--bind_ip_all"]
      ports:
        - "27017:27017"
      restart: always
    django:
      build: .
      command: bash -c "
          python service_platform/manage.py makemigrations &amp;&amp;
          python service_platform/manage.py migrate &amp;&amp;
          python service_platform/manage.py runserver 0.0.0.0:8000
        "
      volumes:
        - .:/code
      ports:
        - "8000:8000"
      depends_on:
        - postgres
        - mongodb
      links:
        - postgres
        - mongodb
  </code></pre>
  
  <p>Below are the db settings of django's settings.py</p>
  
  <pre><code>DATABASE_ROUTERS = [
      'content_manager.content_db_router.ContentDBRouter',
  ]
  
  DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql',
          'NAME': 'postgres',
          'USER': 'postgres',
          'HOST': 'postgres',
          'PASSWORD': 'test',
          'PORT': 5432,
      },
      'content': {
          'ENGINE': 'djongo',
          'NAME': 'mongodb',
          'CLIENT': {
              'host': 'mongodb',
              'port': 27017,
          },
  
      }
  }
  </code></pre>
  
  <p>Here's the custom router I wrote</p>
  
  <pre><code>class ContentDBRouter(object):
  
      content_db = 'content'
      default_db = 'default'
  
      def db_for_read(self, model, **hints):
          if model._meta.app_label == 'content_manager':
              return self.content_db
          return None
  
      def db_for_write(self, model, **hints):
          if model._meta.app_label == 'content_manager':
              return self.content_db
          return None
  
      def allow_migrate(self, db, app_label, model_name=None, **hints):
          if db == self.content_db:
              return True
          elif db == self.default_db:
              return True
          else:
              raise Exception(f'DB with id {db} not found!')
  </code></pre>
  
  <p>and finally this is the model I want to get migrated to the mongo db</p>
  
  <pre><code>class Post(models.Model):
      id = models.AutoField(primary_key=True)
      title = models.TextField(default='default title')
  </code></pre>
  
  <p>When I try to insert post into the db I get the following error rendered in the browser</p>
  
  <pre><code>MigrationError at /
  Table content_manager_post does not exist in database
  </code></pre>
  
  <p>and the following error in the terminal running the containers with the dbs and the django app.</p>
  
  <pre><code>raise MigrationError(f'Table {collection} does not exist in database')
  </code></pre>
  
  <p>Can I get some help to properly configure mongo and postgres in order to use them.
  Initially I tried to run the app without default db and custom routers for each of the models I have, but then reverted to this state in order to properly configure one non default db.
  I also tried wiping out the whole docker volumes and migrations and run it from scratch in order to apply migrations, but I still didn't manage to get it work.</p>