In the Python Django framework, Migration of Database Model is used for making database tables using model in Django. Django makes the tables of models through python code in Django. When user creates direct table in database without Django code then Django doesn’t give all predefined features to that table. But when creating tables through Django model then it gives many features like: create, retrieve, update and delete records and also gives the facility to add this table model in admin panel.
First create django project. If you don’t know how to create a django project follow this URL.
After successful creation of django project now we going to create models step.
- Now open the models.py file which is already created in django app folder. My django app folder's name is “main”.
Note
In python language indentation is very important, if you not follow indentation of code your project or program will not run. You will get this error most times in python. So always use an editor that knows python language indentation otherwise use 4 spaces below of this symbol colon (:) in any editor. Example is shown in below snapshot.
- After open models.py and write code as below,
Remember the Indentation from class.
- Where ->
- from django.db import models define a subclass of model
- UserProfile denotes the table name.
- class attributes define database fields like : name, address, city, state, country
- models.Charfield denote varchar type of data field
- max_length= is argument which specifies the size of VARCHAR data field.
- Django have many type of data field and many type of argument which make tables according to type.
We can make any type of model from django code. Django also has very Short techniques that make models flexible.
This table create database query will be as shown below:
- CREATE TABLE "main_userprofile" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name"
- varchar(30) NOT NULL, "address" varchar(50) NOT NULL, "city" varchar(60) NOT NULL
- , "state" varchar(30) NOT NULL, "country" varchar(50) NOT NULL);
Note
Django always creates ID field in tables always with “NOT NULL PRIMARY KEY AUTOINCREMENT”. So if you have any need for it declare ID field according to you.
- Now we go for further process. Open Command Prompt with directory of root folder of project where you can see manage.py file.
- Django uses SQLite database by default in configuration of settings.py file.
We can use any database with django like: MySQL, MSSQL, Oracle and postgreslq. Django works better with PostgreSql because it have some featured libraries inbuilt for this database.
- After open Command Prompt write command : “python manage.py makemigrations <write your django app name>”.
Ex: “python manage.py makemigrations main”.main is my django app name.
This command create migration folder in django app folder that have file 0001_initial.py that contain UserProfile model definition. Here 0001 is important for further use in model. I will tell you in Next tutorials.
- After Again write command : “python manage.py sqlmigrate <write your django app name> <Migration file prefix number number>”
EX: “python manage.py sqlmigrate main 0001”. Here 0001 denote prefix of 0001_initial.py file name.
This above image show query of table creation that have ID field automatic generated. This command makes table query.
- Last step is for making table in database; write command : “python manage.py migrate”,
This command creates table in database .When you type this command you will see in second line.
Apply all migrations : main, admin, auth, contenttypes, session -> these are the inbuilt model in django that create tables automatic when you type this command first time. There table have many features in django which already predefine in it.
When I open sqlite database you will see these tables as below image:
Here “main_userprofile” is table name of UserProfile Model in django. Here “main” django app name.You can change it by.
Now you have Successfully migrated Django model. So then we can change this model's attributes like adding or removing attributes then run always two command.
Above all commands use the first time when you create first-time tables in database using django. But after you can only use:
- Python manage.py makemigrations
- Python manage.py migrate
Both commands are used always when you edit models. If you want to add another model then use both these commands. There is no requirement of other command.
Issues:
- Don’t delete migration folder files. Otherwise models lose their connectivity to database.
- No need for creating ID field in each table. Django creates it automatically.
- If you want to delete table in database. Remove table model code in models.py and run above both command then table will delete from database. If you delete table manually that will create problem with model. So don’t manually delete database table.