django using database code example

Example 1: use django with another database

DATABASES = {
    'default': {},
    'users': {
        'NAME': 'user_data',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'mysql_user',
        'PASSWORD': 'superS3cret'
    },
    'customers': {
        'NAME': 'customer_data',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'mysql_cust',
        'PASSWORD': 'veryPriv@ate'
    }
}

Example 2: django creating database

# --------------- Start with databases in Django ------------------ #

In your virtual environment, where your Django project lives, 
use the following commands:
  
  # Migrations are Django’s way of propagating changes you make to 
  # your models (adding a field, deleting a model, etc.) into your 
  # database schema.
   >> python3 manage.py makemigrations   
  
  
  # If used for the first time, it creates a standard user model 
  # (a table for saving information about users). Otherwise, it updates 
  # the database with the new information in the folder "migrations" 
  # (responsible for applying and unapplying migrations):
   >> python3 manage.py migrate 
    
    
  # For printing the SQL code that is going to run:
  >> python3 manage.py sqlmigrate "name_app" "code_of_specific_object"  
  # for example: python3 manage.py sqlmigrate app 0001
  
  
  # Run a Django + python shell for working/testing with models:
  >> python3 manage.py shell