7.5. Database

7.5.1. Database schema migration

7.5.2. Makemigrations

  • --squash

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

7.5.3. Migrate

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

7.5.4. Management commands

7.5.5. dbshell

7.5.6. dumpdata

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

7.5.7. loaddata

7.5.8. inspectdb

7.5.9. makemigrations

7.5.10. migrate

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

7.5.12. PostgreSQL

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

7.5.13. Heroku

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

7.5.14. Fixtures

fixtures directory of every installed application

7.5.15. Multiple DB and db routing