how you can set up the Database in Django

Last updated 1 year, 8 months ago | 356 views 75     5

Tags:- Django

Django | Set up Database

We need to edit the setting.py file present in the project, which contains all the settings related to the project.

By default Django comes with an SQLite database; The  SQLite database is available in the project after running the migrations commands:

python manage.py makemigrations
python manage.py migrate

SQLite database is available as a file on your system. Moreover, the name should be as a fully absolute or exact path along with the file name of the particular file.

The setting for SQLite database is already configured in the Django project, here is the sample code:

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

 

if your database choice is different then you need to follow certain keys in the DATABASE like default items to match database connection settings.

  • Engines: These are some engines for different databases:
    • ‘django.db.backends.postgresql_psycopg2’,
    • ‘django.db.backends.sqlite3’,
    • ‘django.db.backends.oracle’,
    • ‘django.db.backends.mysql’, and so on.
  • Name: This represents the name of your own database. 
  • if we are not using SQLite database then additional settings such as password, user, and the host must be added.

 

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': 'DB_HOST',
        'NAME': 'DB_NAME',
        'USER': 'DB_USER',
        'PASSWORD': 'DB_PASS',
    }
}

 

Tips and Tricks


Python In-place swapping of two numbers

 Python | In-place swapping of two numbers

>>> a, b = 10, 20
>>> print(a, b)
10 20

>>> a, b = b, a
>>> print(a, b)
20 10

 


Reversing a String in Python

Python | Reversing a String

>>> x = 'PythonWorld'
>>> print(x[: : -1])
dlroWnohtyP

 


Python join all items of a list to convert into a single string

Python | Join all items of a list to convert into a single string

>>> x = ["Python", "Online", "Training"]
>>> print(" ".join(x))
Python Online Training

 


python return multiple values from functions

Python | Return multiple values from functions

>>> def A():
	return 2, 3, 4

>>> a, b, c = A()

>>> print(a, b, c)
2 3 4

 


Python Print String N times

Python | Print String N times

>>> s = 'Python'
>>> n = 5

>>> print(s * n)
PythonPythonPythonPythonPython