ManyToManyField using specific database

7 views
Skip to first unread message

João Dias de Carvalho Neto

unread,
Jun 23, 2015, 3:40:06 PM6/23/15
to django...@googlegroups.com
Hi all,

I have an application with Django with database routers. Basically I got the request path, split it to get subdomain name and set the database by this way.

I do not use default database, so I do not migrate it.

This route action It's working very well but I am still facing trouble when I am using post_migration signals with post_migrate signal callbacks. 

My custom command to something like above:

def define_groups_auth(sender, **kwargs):
    admin = Group()
    admin.name = "Administrator"
    admin.save(using=kwargs['using'])
    
    perm = Permission.objects.using(kwargs['using']).get(codename='my_permission')
    admin.permissions.add(perm)

post_migrate.connect(define_groups_auth,
                     sender=apps.get_app_config('my_application'))

When I execute ./manage.py --database=db1 for example a strange behavior occurs.

The Group Administrator is created but the permission tries to be added on default database instead db1 database.

hacking the "add" method i discover that it do not contains a "using" parameter. 

Does someone knows a work around to save this ManyToMany relation correctly on post_migrate signals?


Reply all
Reply to author
Forward
0 new messages