Database connectivity using Django:-

To manage database operation in Django  First We create a Database using SQLITE, It will be by default that exists under the Django project.

SQLite is a database software tool which is the RDBMS light-weight package which is mostly used in mobile application 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.

ORM means Object-relational mapping tools that provide a set of classes to implement a set of tables.

Step for database Operation using DJANGO

1)  Create an app using python startup app name

2)  Create Classes under  according to a database table for example if we want to create 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

   python migrate  #to enable all predefine app under

   python makemigrations appname  #it is used to enable the current app for migration, it will convert the model class to the table schema

 4) run command to sqlmigrate

   python sqlmigrate appname 0001  # it will convert table schema to SQL Script

   After the successful execution of this command, it will create a table SQL Script.

 5) python migrate  #it will create table using SQL Script

 6)  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

 7) to login into  the super admin dashboard run the following command

     python createsuperuser  #it provide a console screen to write username and password to admin

 8) open admin dashboard using  server should be started

 9)  login with a valid username and password

 10) add registration model class  of application to the admin dashboard

 11)  go into and write the following code

      from django.contrib import admin

      from .models import Register

12) refresh the admin dashboard your table will be visible

13) data will be shown on Object pattern 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 in your machine.

    pip install django mysqlclient

2) go into base project and change the database list.

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'databasename',
'USER': 'root'
'HOST': '127.0.0.l'
'PORT':'3306' } }

remaining command are the same 

python makemigrations
python migrate
 python sqlmigrate appname 0001 
python migrate

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post