20. Database

20.1. Database schema migration

20.2. Makemigrations

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

20.3. Migrate

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

20.4. Management commands

20.5. dbshell

20.6. dumpdata

dumpdata --format json --exclude=auth --exclude=contenttypes

20.7. loaddata

20.8. inspectdb

20.9. makemigrations

  • --squash

20.10. migrate

20.11. Database settings

20.12. 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'),
    }
}

20.13. PostgreSQL

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

20.14. Heroku

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

20.15. Fixtures

fixtures directory of every installed application

20.16. Multiple DB and db routing