Search code examples
pythondatabasedjangodjango-settings

Module doesn't define an attribute/class


I want to write a multi-table application on Django, so I created two databases, one of them to use by default, other - "map" to use by specific app - "map".

map/models.py:

from django.db import models
class MapRouter(object):
   def db_for_read(self, model, **hints):
               if model._meta.app_label == 'map':
                       return 'map'
               return None
   def db_for_write(self, model, **hints): 
               if model._meta.app_label == 'map':
                       return 'map'
               return None
   def allow_relation(self, obj1, obj2, **hints):
               if obj1._meta.app_label == 'map' or \
                  obj2._meta.app_label == 'map':
                  return True
               return None
   def allow_migrate(self, db, model):
               if db == 'map':
                       return model._meta.app_label == 'map'
               elif model._meta.app_label == 'map':
                       return False
               return None

settings.py:

DATABASES = {
   'default': {
       'ENGINE': 'django.db.backends.postgresql_psycopg2',
       'NAME': 'eventmap',
       'USER': 'eventmap',
       'PASSWORD': 'eventmap',
       'HOST': 'localhost',
       'PORT': '',
   },
   'map': {
       'ENGINE': 'django.db.backends.postgresql_psycopg2',
       'NAME': 'map',
       'USER': 'eventmap',
       'PASSWORD': 'eventmap',
       'HOST': 'localhost',
       'PORT': '',
   }
}
DATABASE_ROUTERS = ['map.MapRouter']

The problem is when I run python manage.py syncdb it says:

django.core.exceptions.ImproperlyConfigured: Module "map" does not define a "MapRouter" attribute/class

What's wrong with it?


Solution

  • You need to put the full path to your router in the setting.

    DATABASE_ROUTERS = ['map.models.MapRouter']