To manage database operation in Django First We create a Database using SQLITE, It will be by default exists under the Django project.
SQLite is a lite weight database software which is the RDBMS (Relational database management system) light-weight package which is mostly used in mobile applications but now it can be used in the web application, enterprise application, etc.
Django provides ORM (Object Relational Mapping) tools that are used to map the database table of SQLite in an application using objects.
ORM means Object-relational mapping that provides a set of classes to implement a set of tables.
Step's for database Operation using DJANGO
1) Create an app using python manage.py startapp app-name
2) Create Classes under models.py file according to a database table schema for example if we want to create a registration form using four different fields username, password, email, and mobileno then we create Register class and define all fields.
class Register(models.Model):
emailid= models.CharField(max_length=200)
password= models.CharField(max_length=200)
mobile= models.CharField(max_length=200)
fullname= models.CharField(max_length=200)
3) run command to migrate model class to the table schema
3.1) python manage.py migrate #to enable all predefine app under settings.py INSTALLED_APPS Section.
3.2) python manage.py makemigrations appname #it is used to enable the current app for migration, it will convert the model class to the table schema
4) python manage.py migrate #it will create table using SQL Script
5) If we want to show a table then we should go into the super admin dashboard, which will be by default exist under the Django framework. we should create user id and password only
6) to login into the super admin dashboard run the following command
python manage.py createsuperuser #it provides a console screen to write username and password to admin
7) open admin dashboard using 127.0.0.1:8000/admin server should be started
8) login with a valid username and password
9) add a registration model class of application to the admin dashboard
10) go into admin.py that exists under app and write the following code
from django.contrib import admin
from .models import Register
admin.site.register(Register)
11) refresh the admin dashboard your table will be visible
12) data will be shown on Object pattern in admin dashboard so we can Override __str__ method in the class and convert an object to String.
How to work with another database such as MySQL:-
1) First, install mysqlclient on your machine.
open command prompt and type this command
pip install mysqlclient==2.2.4
or
pip install mysqlclient
download XAMPP Server and install it, start apache and MySQL using XAMPP Control Panel.
IF Mysql is already installed then no need XAMPP Server.
for xampp open browser and type localhost/phpmyadmin
then create database .
2) go into base project settings.py and change the database list.
If not work then try this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'testdb',
'USER': 'root',
'PASSWORD':"",
'HOST':"127.0.0.1",
'PORT':"3306",
'OPTIONS': {
'ssl_mode': 'DISABLED',
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
remaining command are the same
python manage.py makemigrations
python manage.py migrate
python manage.py migrate
python manage.py sqlmigrate appname 0001
python manage.py migrate
POST Answer of Questions and ASK to Doubt