0

I just tried to find a solution for define database model wise in Django. like in my settings.py there are three databases

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'db_one': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db_one.sqlite3'),
    },
    'db_two': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db_two.sqlite3'),
    },
}

And in my polls/models.py

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')


    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

@python_2_unicode_compatible
class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)


    def __str__(self):
        return self.choice_text

now I want to add question model in db_one database and Choice model in db_two database so how can I do that

I try with routers follow this multiple databases and multiple models in django but it prefers only default database only after that I tried with put the blank setting in default database and try to migrate but it gives me an error

or

is that way to define a database for application wise in Django

Kartik
  • 65
  • 9

2 Answers2

0

you can use database my using method.

For example, fetching all data from db_one would look like:

all_data = YourModel.objects.using('db_one').all()

sebb
  • 1,956
  • 1
  • 16
  • 28
  • fetching data is coming later in the picture first I just want to create that table to database wise the problem is I am not able to migrate that table(create table) – Kartik Aug 02 '17 at 09:58
0

You need to migrate your model to a database other than the default, right?

You can select the database while migrating.

# database name from settings.py
./manage.py migrate --database=<database-name>

django docs

If you want to keep some tables in one database and some tables in other databases, I suggest to create a migration per table. Then, you will be able to run

./manage.py migrate <your-app-name> <migration-number> --database=<database-name>

EDIT

You can skip the creation of a specific table in a specific database with --fake flag.

  • first, remove all of your migrations.
  • comment out your Choice model in models.py.
  • run manage.py makemigrations => suppose this created migration 001 (for Question model)
  • uncomment you Choice model in models.py.
  • run manage.py makemigrations again. suppose this created migration 002 (for Choice model)
  • run manage.py migrate <app-name> 001 --database=db_one => this will create Question model in db_one
  • run manage.py migrate <app-name> 001 --database=db_two --fake => this
    will skip the creation of the table in db_two
  • run python manage.py migrate <app-name> 002 --database=db_one --fake => this will skip the creation of the Choice table in db_one
  • run python manage.py migrate <app-name> 002 --database=db_two => this will create Choice model in db_two.
ohannes
  • 514
  • 7
  • 10
  • no, I want to use the separate model of one app when I migrate table so it will create only default databases – Kartik Aug 02 '17 at 10:11
  • what do you mean by separate? keeping copies of the tables in different databases? – ohannes Aug 02 '17 at 10:12
  • no, i don't want to create a copy of Question model is only create in db_one and a Choice table is only create in db_two no in default or any other database – Kartik Aug 02 '17 at 10:18
  • ok, then, as I pointed in the second part, you need to create a migration per each model. Then, run migrations on databases selectively. I will update my answer to show how you can skip the creating of a table on a specific database. actually you need to use --fake option for this. – ohannes Aug 02 '17 at 10:23