Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Database connectivity using Django:-

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 for database Operation using DJANGO


1)  Create an app using python manage.py startup app-name


2)  Create Classes under models.py  according to a database table 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 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'
'PASSWORD:''
'HOST': '127.0.0.l'
'PORT':'3306' } }

remaining command are the same 

python manage.py makemigrations
python manage.py migrate
 python manage.py sqlmigrate appname 0001 
python manage.py migrate






Post a Comment

POST Answer of Questions and ASK to Doubt

Previous Post Next Post