set up the Database in Django

Python Programming Features, Python Syntax, Comments, Variables, Operators, Control Flow, Strings, Arrays, IO & File Handling, Python Object Oriented Programming.
Post Reply
charan
Posts: 48
Joined: Fri Nov 23, 2018 5:31 am

set up the Database in Django

Post by charan » Thu Dec 06, 2018 7:49 am

Explain how you can set up the Database in Django ?

swathi
Posts: 134
Joined: Fri Nov 23, 2018 2:52 am

Re: set up the Database in Django

Post by swathi » Thu Dec 06, 2018 7:51 am

You can use the command edit mysite/setting.py , it is a normal python module with module level representing Django settings.

Django uses SQLite by default; it is easy for Django users as such it won’t require any other type of installation. In the case your database choice is different that you have to the following keys in the DATABASE ‘default’ item to match your database connection settings.

1) Engines: you can change database by using ‘django.db.backends.sqlite3’ , ‘django.db.backeneds.mysql’, ‘django.db.backends.postgresql_psycopg2’, ‘django.db.backends.oracle’ and so on

2) Name: The name of your database. In the case if you are using SQLite as your database, in that case database will be a file on your computer, Name should be a full absolute path, including file name of that file.

3) If you are not choosing SQLite as your database then settings like Password, Host, User, etc. must be added.
Django uses SQLite as default database, it stores data as a single file in the filesystem. If you do have a database server—PostgreSQL, MySQL, Oracle, MSSQL—and want to use it rather than SQLite, then use your database’s administration tools to create a new database for your Django project. Either way, with your (empty) database in place, all that remains is to tell Django how to use it. This is where your project’s settings.py file comes in.

We will add the following lines of code to the setting.py file:

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

Post Reply

Return to “Python Programming Fundamentals”