Django & MySQL Database


installing mysqlclient

Mysqlclient is an adopter for Python to work with MySQL database.
pip install mysqlclient
We will create one project plus2net
django-admin startproject plus2net
This will start a project plus2net. Now we will create a directory and place some files. Inside plus2net directory we have one , this we will use to run our server

change directory to plus2net
cd plus2net
Now run the server
python runserver
Once the server is running we can see the home page. Now open the URL
Creating an app We will create one app in the name student.
python startapp student

Connecting to MySQL

Our is located inside plus2net directory. Inside this file we will store our MySQL connection details.
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django',
		'USER' : 'userid',
		'PASSWORD' :'password',
		'HOST' : 'localhost'

Creating a model

class student(models.Model):
	st_name =models.CharField(max_length=100)
	st_sex  =models.CharField(max_length=20)
	st_mark =models.IntegerField()
We need to add this app to our settings
python migrate
python makemigrations
Once the migration is done , we will get a file inside migration folder
# Generated by Django 2.2.5 on 2020-01-13 02:20

from django.db import migrations, models

class Migration(migrations.Migration):

    initial = True

    dependencies = [

    operations = [
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('st_name', models.CharField(max_length=100)),
                ('st_class', models.CharField(max_length=20)),
                ('st_sex', models.CharField(max_length=20)),
                ('st_mark', models.IntegerField()),
python sqlmigrate student 0001_initial
It will show us one SQL statement. Here it is
-- Create model student
CREATE TABLE `student_student` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `st_name` varchar(100) NOT NULL, `st_class` varchar(20) NOT NULL, `st_sex` varchar(20) NOT NULL, `st_mark` integer NOT NULL);
This will not create tables in our MySQL database , we have to do that now.
python migrate
This will run all our migrations and you will see screen like this
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, student
Running migrations:
  Applying student.0001_initial... OK
Now this will create tables inside our database ( Check the database name you have given inside file ).

Note that this table is created with the help of Django. We have not written any SQL code to create the table. Check your PhpMyAdmin to verify the table.

Adding data to table

We can create one superuser and use the Django Admin interface to add data to our student table.

Admin to add data to student table Display data from MySQL table
Web form data inserting to MySQL table Adding multiple records to MySQL table
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer