17. Database

17.1. Database schema migration

17.1.1. Makemigrations

$ python manage.py makemigrations
Migrations for 'contact':
  addressbook/contact/migrations/0001_initial.py
    - Create model Contact

17.1.2. Migrate

$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, contact, sessions
Running migrations:
  Applying contact.0001_initial... OK

17.2. Management commands

17.2.1. dbshell

17.2.2. dumpdata

dumpdata –format json –exclude=auth –exclude=contenttypes

17.2.3. loaddata

17.2.4. inspectdb

17.2.5. makemigrations

  • --squash

17.2.6. migrate

17.3. Database settings

17.3.1. Sqlite3

# https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'HabitatOS.sqlite3'),
    }
}

17.3.2. PostgreSQL

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

17.3.3. Heroku

if os.environ.get('DATABASE_URL'):
    import dj_database_url
    DATABASES['default'] = dj_database_url.config()

17.4. Fixtures

fixtures directory of every installed application

17.5. Multiple DB and db routing