User:Niraj/Teaching-25

Teaching lesson plan 25 Subject: Python programming

Date: 12 Feb 2024

Time: 60 minutes

Period: 3rd

Teaching Item: Introduction to Django Models

Class: Bachelor

Objective:

Students will learn the fundamentals of Django models, including defining models, creating database tables, performing CRUD operations, and understanding relationships between models.

Materials Needed:

  • Python installed (preferably Python 3.x)
  • Text editor or integrated development environment (IDE) such as VSCode, PyCharm, or Sublime Text
  • Django framework installed (pip install django)
  • Projector

1. Introduction to Django Models (15 mins)

  • Overview of Django models:
    • Django models represent database tables and their relationships.
    • Models are defined using Python classes that inherit from django.db.models.Model.
  • Discuss the importance of models in Django projects and their role in data persistence.

2. Defining Models (20 mins)

  • Creating a Django app for models: python manage.py startapp myapp
  • Defining model classes:
    • Define model fields to represent database columns.
    • Specify field types (e.g., CharField, IntegerField, ForeignKey).
    • Add meta options such as verbose_name and verbose_name_plural.
  • Example: Creating a Book model with fields like title, author, published_date, etc.

3. Migrations and Database Schema (20 mins)

  • Generating migrations: python manage.py makemigrations
  • Applying migrations to create database tables: python manage.py migrate
  • Understanding migration files and their role in database schema evolution.
  • Discussing how Django manages database schema changes over time.

4. Performing CRUD Operations (25 mins)

  • Creating records:
    • Using model classes to create new records.
    • Saving records to the database using the save() method.
  • Retrieving records:
    • Querying records using the model's objects manager.
    • Filtering records based on specific criteria using filter(), get(), and exclude().
  • Updating records:
    • Retrieving records, modifying their attributes, and saving them back to the database.
  • Deleting records:
    • Deleting records using the delete() method or bulk delete operations.

5. Model Relationships (25 mins)

  • One-to-Many (ForeignKey) relationship:
    • Defining relationships between models using ForeignKey fields.
    • Understanding the concept of related objects and reverse relationships.
  • Many-to-Many relationship:
    • Defining many-to-many relationships using ManyToManyField.
    • Accessing related objects using querysets and reverse relationships.
  • One-to-One relationship:
    • Defining one-to-one relationships using OneToOneField.
    • Use cases and examples of one-to-one relationships.

6. Exercise: Create and Manipulate Models (20 mins)

  • Provide a hands-on exercise where students create Django models and perform CRUD operations.
  • Task:
    • Define multiple models representing real-world entities (e.g., Book, Author, Publisher).
    • Create relationships between models (e.g., Book has an Author).
    • Write Python code to create, retrieve, update, and delete records using Django models.

7. Conclusion (10 mins)

  • Recap the key points covered in the lesson:
    • Django models represent database tables and define the structure of data.
    • Migrations are used to manage changes to the database schema over time.
    • CRUD operations (Create, Retrieve, Update, Delete) can be performed using Django model objects.
    • Relationships between models enable complex data modeling and querying.
  • Encourage students to practice creating models and performing CRUD operations in their Django projects.