2

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?

6
  • I don't know django well enough, but "map" is a builtin function. I suspect its imported rather than the module you want. Don't name modules after built in functions! Commented Sep 19, 2014 at 15:47
  • I've changed this name to "mymap", but still have this exception( Commented Sep 19, 2014 at 15:53
  • does that app have an __init__.py file ? Commented Sep 19, 2014 at 15:59
  • In the mymap directory, do you have a __init__.py and does it from models import MapRouter? Maybe there is other django magic, but map.MapRouter doesn't exist unless your __init__.py makes it so. Commented Sep 19, 2014 at 16:00
  • Yes, it have that file and all other files that are common for django apps Commented Sep 19, 2014 at 16:03

1 Answer 1

5

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

DATABASE_ROUTERS = ['map.models.MapRouter']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.