Django’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:
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.
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.namename: 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.Once the model is defined, we need to create and apply database migrations.
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.
Apply the changes to the database:
python manage.py migrateThis creates the corresponding table in the database.
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.
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)student = Student.objects.get(id=1)
student.name = "Rahul Verma"
student.save()student = Student.objects.get(id=1)
student.delete()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.
Sign in to join the discussion and post comments.
Sign inPython Basics
Python is a powerful, high-level programming language known for its simplicity and versatility. It is widely used in various fields, including web development, data science, artificial intelligence, automation, and more. This tutorial series is designed to take you from the basics of Python to more advanced topics, ensuring a strong foundation in programming.
Object-Oriented Programming (OOP) in Python
Learn the fundamentals of Object-Oriented Programming (OOP) in Python, including classes, objects, inheritance, polymorphism, encapsulation, and more. Understand how OOP enhances code reusability, scalability, and organization.