Django Database


Introduction to Django Database

Django uses SQL, a Structured Query Language, to perform different operation crud operations. Typically SQL contains the complex query for creating, updating, and deleting data. Django supports databases such as Oracle, MySQL, SQLite, PostgreSQL, and MariaDB; we can use anyone per our requirements. On the other hand, Django supports many other features of all types of databases. For example, Django usually has an ORM feature, Object Relational Mapper; it allows the developer to map the data between the Django application model and database without any SQL queries; the ORM uses the model classes to interact with data.

Overview of Django Database

The Django structure is a free, open-source system that fosters web applications. This Python-based structure comprises various parts like verification, content administration, and data set communications that you can use to make any web application.

Django’s most impressive and valuable highlight is its Object-Relational Mapper (ORM). The ORM permits designers to plan information between the application models and the data set without composing any SQL questions. For example, the Object-Relational Mapper will plan object credits characterized in Django model classes to the comparing fields in the data set and communicate with the information as essential from a given data set. This allows engineers to quickly foster their applications, eliminating the need to compose separate SQL questions for information base exchanges.

The Django system Model-View-Template (MVT) engineering is planned so designers can change the frontend visual construction of an application without influencing the fundamental backend rationale. This is particularly valuable while connecting with information bases. When a Django web application is designed, an SQLite information base is naturally made.

How to Connect Django Database?

Now let’s see how we can connect the Django database as follow:

We know that Django supports different types of databases; here, we will see how we can connect Django with MySQL as follows:

First, we need to consider the following prerequisites as follows:

  • Installation of MySQL server with 5.7 + version
  • Installation of Python 3.0+ version

Here consider we already installed MySQL and Python and follow the below steps as follows:

First, we need to create a virtual environment with the help of the below command as follows:

Code:

mkdir stud_management
cd stud_management

After that, we need to set the virtual environment for the newly created project, so we need to install the virtual environment using the below command.

Code:

pip install virtualenv
virtualenv env

Now we have a virtual environment, so start the Django project using the below command.

Code:

django-admin startproject stud_management
cd stud_management
django-admin startapp student

Now we can check the setting.py file of our application, as shown below code.

Code:

INSTALLED_APPS = [
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  ' student ',
]

Now we need to run the server with the help of the below command as follows.

Code:

python manage.py runserver

After executing the above command, we get an IP address on the terminal, as shown below screenshot.

Output:

IP address on the terminal

Next, we need to create a new database in MySQL workbench, or we can use the shell for creating a database.

After login into MySQL, we will get the following screen as shown below screenshot as follows.

login into MySQL

Here we need to create the database using SQL queries per our requirement and update the setting.py file after that.

Creating Django Database

Now let’s see how we can create a database in Django as follows:

Here we will see the MySQL database in different steps as follows:

Of course, when we made our first application and began the server, you probably saw another record in your task registry named ‘db.sqlite3’. The record is an information base document where every one of the information that you will create will be put away as shown below screenshot as follows.

manage

This database file was automatically created when we created the Django application, and it has a default setting of the data set to the SQLite, which is albeit fine for testing and gives loads of highlights; however, if you maintain that your site should be versatile, you want to transform it to some other proficient data set.

First, we need to make some changes in the setting.py file as follows:

Code:

DATABASE = 
  'deafult': 
    'ENGINE':django.db.backends.sqlite3, 'NAME':os.path.join(DIR,'db,sqlite3'),
    
    

Explanation:

In the above example, we can see how to establish a database connection; here, we have two different terms as follows:

  • ENGINE: It determines the library used when associated with a specific site. In the end, we need to put the record, “Django.db.backends.sqlite3”, which is the python library for the sqlite3 data set and will make an interpretation of your python code to the data set language.
  • NAME: Here, you will have the name of the information base you are utilizing and the area of your data set. This boundary changes as indicated by the information base you are utilizing. Here you can explore different avenues regarding the information base document.

Now we need to execute the below command as follows:

Code:

python manage.py migrate
python manage.py runserver

Explanation:

  • After executing the above command, we see a new database was created, as shown in the screenshot below.

new database was created

This is the first and straightforward way to create a database; now, let’s see another method for MySQL as follows.

First, we need to open MySQL on the terminal and connect with the server, as shown in the below screenshot.

Django Database 5

Now here, we need to create a new database with the help of the below screenshot as follows:

Code:

create database dsample;

Explanation:

  • After execution of the above query, we can see the newly created database as shown below screenshot as follows:

newly created

Now we need to update the setting.py file as follows:

Code:

DATABASES = 
  'default': 
   'ENGINE': 'django.db.backends.mysql',
   'NAME': 'dsample',
   'USER': 'root',
   'PASSWORD': '******',
   'HOST': '127.0.0.1',
   'PORT': '8080',
  

After that, we need to install mysqlclient packages with the below command as follows:

Code:

pip install mysqlclient

Now run the migration command.

Finally, change the database as per our requirements.

Conclusion

With the help of the above article, we saw about the Django database. From this article, we saw basic things about the Django database and the features and installation of the Django database, and how we use it in the Django database.

Recommended Articles

This is a guide to Django Database. Here we discuss the introduction and how to connect and Create a Django database. You may also have a look at the following articles to learn more –

  1. Django CMS
  2. Django one to many
  3. Django Template Tags
  4. Django Annotate

The post Django Database appeared first on EDUCBA.



Source link

Leave a Reply

Your email address will not be published.