2

I would like to ask how to set models to specific database . I'm still new to django and read about the Database Routing in the Django website, I have 2 models, userMod and adminMod.
userMod should go to database userDB.
adminMod should go to database adminDB.
But when I migrated it, both table exist in both database. I already have included app_label on meta, but its still not working. I am using django 1.8


EDIT: I am just trying it on userDB for a while and eventually will use it with adminDB. This is my code:

routers.py

class router(object):

def db_for_read(self, model, **hints):
    if model._meta.app_label == 'userDB':
        return 'userDB'
    return None

def db_for_write(self, model, **hints):
    if model._meta.app_label == 'userDB':
        return 'userDB'
    return None

def allow_relation(self, obj1, obj2, **hints):
    if obj1._meta.app_label == 'userDB' or\
        obj2._meta.app_label == 'userDB':
            return True
        return None

def allow_migrate(self, db, app_label, model=None, **hints):
    if app_label == 'userDB':
        return db == 'userDB'
    return None
2
  • Hi, please insert your code for more clarity. Read this to improve your question so that we can help you better. stackoverflow.com/help/mcve Commented Jul 21, 2015 at 4:18
  • Could you paste the code of your database router? Commented Jul 21, 2015 at 5:20

2 Answers 2

1

Try this:

class userMod(models.Model):
   name = models.CharField(max_length=120)

   class Meta:
      db_table='userDB'
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Geo Jacob. Thank you for the suggestion but unfortunately this didn't work. Both tables still exist in userDB
I didn't tried this, but this may help you. stackoverflow.com/questions/18547468/…
1

Remember to tell django about your database router in the settings.py

https://docs.djangoproject.com/en/1.8/topics/db/multi-db/

Search for DATABASE_ROUTERS setting

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.