1

I have multiple Django projects. Each one has its own installed apps. They do not have the same apps installed.

The thing in common between them all, is that they should use and share the same Custom User model and database.

Which is the right way to accomplish this?

Thanks!

GonzaloN
  • 45
  • 7

1 Answers1

1

This is a very tricky problem unless you designate exactly one of your projects to be the "master" of the custom user model. Otherwise, you might have competing migrations against the same database table. Once you decide which project will be the master, the non-master projects should have a separate entry in DATABASES that will connect them to the central user database


DATABASES = {
    'default': ...
    'users': {
        'HOST': 'central-host.example.com',
        'NAME': 'central-users',
        ... 
    }
}
DATABASE_ROUTERS = 'my_app.database_router'

In your router, you can specify that the User model should be read from the central-users database:


class Router(object):
    def db_for_read(self, model, **hints):
        if model.__name__ == 'CustomUserModel':
            return 'users'
        return 'default'

    def db_for_write(self, model, **hints):
        if model.__name__ == 'CustomUserModel':
            return 'users'
        return 'default'

2ps
  • 15,099
  • 2
  • 27
  • 47
  • It feels somehow like an overkill. Wouldn’t be enough to declare manage=false in the children project user model? – GonzaloN Dec 13 '18 at 22:33
  • What shoud I do with the "Chlid project" user model? Should i define one in settings? – GonzaloN Dec 19 '18 at 16:43