- Python for Web Development
-
Overview
- Introduction to Flask and Django
- Setting Up a Flask Application
- Django Models and Migrations
- Routing and URL Handling in Django and Flask
- Forms and User Authentication in Django and Flask
- REST API Development with Flask & Django
- Working with Databases (SQLite, PostgreSQL, MySQL)
- Template Engines (Jinja2 for Flask, Django Templates)
- Deployment of Flask & Django Applications on AWS, GCP, and Heroku
- Security Best Practices for Web Apps
Django Models and Migrations
Add to BookmarkDjango’s ORM (Object-Relational Mapping) allows developers to interact with databases using Python code instead of raw SQL. Models define the structure of your database tables, while migrations help apply changes to the database.
In this tutorial, we will cover:
- What are Django models?
- Defining models in Django
- Running migrations to create database tables
- Working with models in the Django shell
1. What are Django Models?
A model in Django is a Python class that represents a database table. Each attribute in the model corresponds to a column in the database. Django provides built-in fields like CharField, IntegerField, and DateTimeField to define data types.
2. Creating a Django Model
To define a model, open the models.py file inside your Django app and add the following code:
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
email = models.EmailField(unique=True)
enrollment_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.nameExplanation:
name: A text field with a maximum length of 100 characters.age: An integer field.email: An email field that must be unique.enrollment_date: Stores the date and time when the student was added.__str__(): Returns the student’s name when printed.
3. Applying Migrations
Once the model is defined, we need to create and apply database migrations.
Step 1: Create Migration
Run the following command to generate a migration file:
python manage.py makemigrationsDjango will create a migration file inside the migrations directory of your app.
Step 2: Apply Migration
Apply the changes to the database:
python manage.py migrateThis creates the corresponding table in the database.
4. Registering Models in Admin Panel
To manage models in the Django admin panel, register them in admin.py:
from django.contrib import admin
from .models import Student
admin.site.register(Student)Now, run the server and visit http://127.0.0.1:8000/admin/. You will be able to see and manage students in the Django admin panel.
5. Working with Models in Django Shell
Django provides an interactive shell to interact with the database.
Run the shell:
python manage.py shellThen, execute the following commands:
from myapp.models import Student
# Creating a new student
student = Student(name="Amit Sharma", age=22, email="amit@example.com")
student.save()
# Fetching all students
students = Student.objects.all()
print(students)
# Filtering students
young_students = Student.objects.filter(age__lt=25)
print(young_students)6. Common Model Operations
Updating a Record
student = Student.objects.get(id=1)
student.name = "Rahul Verma"
student.save()Deleting a Record
student = Student.objects.get(id=1)
student.delete()Conclusion
Django models provide a powerful way to define database structures using Python code. With migrations, you can easily apply changes to your database without manually writing SQL queries.
Prepare for Interview
- JavaScript Interview Questions for 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
Random Blogs
- Datasets for Speech Recognition Analysis
- Deep Learning (DL): The Core of Modern AI
- How AI is Making Humans Weaker – The Hidden Impact of Artificial Intelligence
- What Is SEO and Why Is It Important?
- How to Become a Good Data Scientist ?
- Exploratory Data Analysis On Iris Dataset
- The Beginner’s Guide to Normalization and Denormalization in Databases
- How AI Companies Are Making Humans Fools and Exploiting Their Data
- Where to Find Free Datasets for Your Next Machine Learning & Data Science Project
- Datasets for Exploratory Data Analysis for Beginners
- 10 Awesome Data Science Blogs To Check Out
- Convert RBG Image to Gray Scale Image Using CV2
- Time Series Analysis on Air Passenger Data
- Quantum AI – The Future of AI Powered by Quantum Computing
- Mastering Python in 2025: A Complete Roadmap for Beginners
Datasets for Machine Learning
- Awesome-ChatGPT-Prompts
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset


