In this article, we will create a basic model of an app. Say, we have a project geeksforgeeks in which we will create a new app in order to simplify and make independent model units.
To create an app run command through terminal :
python manage.py startapp geeks
and add geeks to INSTALLED_APPS list in settings.py. Now directory structure of the app will be,

Now go to models.py in geeks app, Here we will create our first model. To create a model you need to first import the Model from django.db.models library.
Now models.py will look like,
# importing Model from django from django.db.models import Model |
According to Django documentation, A model is the single, definitive source of information about your data. It contains the essential fields and behaviours of the data you’re storing. Generally, each model maps to a single database table.
Django provides a number of predefined fields and methods to create a Model. To create a model you need to specify a model name first.
Enter the following code into models.py
from django.db import models from django.db.models import Model # Created an empty model class GeeksModel(Model): pass |
The similar syntax would always be used to create a model.
After making any change in any of app’s models file you need to run following command from the terminal
Python manage.py makemigrations
After this command run following command to finally implement database changes accordingly
Python manage.py migrate
After you run makemigrations and migrate a new table would have been created in database. You can check it from geeks -> makemigrations -> 0001_initial.py.
# Generated by Django 2.2.5 on 2019-09-25 06:00 from django.db import migrations, models class Migration(migrations.Migration): initial = True dependencies = [] operations = [ migrations.CreateModel( name ='GeeksModel', fields =[ ('id', models.AutoField(auto_created = True, primary_key = True, serialize = False, verbose_name ='ID')),], ), ] |
Let’s understand clearly what Makemigrations and Migrate do.
Makemigrations –
makemigrations basically generates the SQL commands for preinstalled apps (which can be viewed in installed apps in settings.py) and your newly created apps’ model which you add in installed apps. It does not execute those commands in your database file. So tables are not created after makemigrations.
After applying makemigrations you can see those SQL commands with sqlmigrate which shows all the SQL commands which have been generated by makemigrations. To check more about makemigrations visit – Django App Model – Python manage.py makemigrations command
Migrate –
migrate executes those SQL commands in the database file. So after executing migrate all the tables of your installed apps are created in your database file.
You can confirm this by installing sqlite browser and opening db.sqlite3 you can see all the tables appears in the database file after executing migrate command. To check more about makemigrations visit Django manage.py migrate command | Python
Recommended Posts:
- Python | Django News App
- Weather app using Django | Python
- How to Create an App in Django ?
- Django model data types and fields list
- How to Create a Basic Project using MVT in Django ?
- How to Create a basic API using Django Rest Framework ?
- Add the slug field inside Django Model
- Render Model in Django Admin Interface
- Django Request and Response cycle - HttpRequest and HttpResponse Objects
- Django Introduction and Installation
- Python | Django-allauth setup and Configuration
- Python Django | Google authentication and Fetching mails from scratch
- Python | Extending and customizing django-allauth
- Django Sign Up and login with confirmation Email | Python
- Difference Between Django and Node.js
- Creating and Using Serializers - Django REST Framework
- Date and time fields in serializers - Django REST Framework
- Top 10 Django Apps And Why Companies Are Using it?
- Handling Ajax request in Django
- Django Introduction | Set 2 (Creating a Project)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : NaveenArora

