How To Migrate File In Django

Python’s Django web framework Django abstracts away much of the complexity when building web applications. One of its key features is to dynamically interact with a given database on a user’s behalf and takes care of many low-level functions that can slow the development process .

What is migrations in Django

Migrations are Django’s way of propagating changes you make to your models into your database schema.

Django solves this problem by using an object-relational mapper to interface with the database on your behalf. In other words, you tell django what structure you want the database to have, and It takes care of translating your Python instructions to SQL queries to be executed on your chosen database. While you can still write SQL if needed and in order to go from data model to database table, you’ll need a migration. This is a special type of Python file that contains the instructions that Django needs to create the database table on your behalf.

It uses your data model to populate these instructions and then runs a migrate command to apply those changes to the database.

  1. Create a migration file containing instructions for altering the database table
  2. Migrate the database table by running the code contained in the migration file

How to create migrations in Django project

create a main project file by using the following Django command

django-admin startproject <project_name>

Here in this blog the project name is migrationfile

Now after creation of the project change the working directory to the project file , The following Django command is used to change the working directory .

cd <project_name>

Here in this blog the project name is migrationfile

Here after creations of the project and changing the working directory we need to create the app , the Django command used for the creation of the app .

python manage.py startapp <app_name>.

Here in this blog the app name is firstmigration .

After creation of the app we need to add the <app_name> in the installed app in the settings section of the project .

settings.py

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

Creation of the models for the migration

Here comes our first step that is create a migration file containing instructions for altering the database table

models.py

from django.db import models

# Create your models here.
class firstmigration(models.Model):
    username=models.CharField(max_length=10)
    phone_number=models.IntegerField()
    email=models.EmailField()

Now we need to add the model_name in the app/admin.py file

admin.py

from django.contrib import admin
from .models import firstmigration

# Register your models here.
admin.site.register(firstmigration)

After adding the <app_name > in the admin.py section use the following Django command to make migration file

python manage.py makemigrations

If the above command is executed then we can find a migrations file in the following section firstmigration/migrations/0001_initial.py section

Here comes our second step that is Migrate the database table by running the code contained in the migration file

Here comes the last step that is checking whether the created model is migrated to database .We can check the model by checking in the admin panel of the Django admin panel page .

Project Github Link — https://github.com/saikumar248/Djangomigrations.git