Building the course models

Our e-learning platform will offer courses on various subjects. Each course will be divided into a configurable number of modules, and each module will contain a configurable number of contents. There will be contents of various types: text, file, image, or video. The following example shows what the data structure of our course catalog will look like:

Subject 1
Course 1
Module 1
Content 1 (image)
Content 2 (text)
Module 2
Content 3 (text)
Content 4 (file)
Content 5 (video)
...

Let's build the course models. Edit the models.py file of the courses application and add the following code to it:

from django.db import models
from django.contrib.auth.models import User

class Subject(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True)

class Meta:
ordering = ['title']

def __str__(self):
return self.title

class Course(models.Model):
owner = models.ForeignKey(User,
related_name='courses_created',
on_delete=models.CASCADE)
subject = models.ForeignKey(Subject,
related_name='courses',
on_delete=models.CASCADE)
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True)
overview = models.TextField()
created = models.DateTimeField(auto_now_add=True)

class Meta:
ordering = ['-created']

def __str__(self):
return self.title


class Module(models.Model):
course = models.ForeignKey(Course,
related_name='modules',
on_delete=models.CASCADE)
title = models.CharField(max_length=200)
description = models.TextField(blank=True)

def __str__(self):
return self.title

These are the initial Subject, Course, and Module models. The Course model fields are as follows:

  • owner: The instructor that created this course.
  • subject: The subject that this course belongs to. A ForeignKey field that points to the Subject model.
  • title: The title of the course.
  • slug: The slug of the course. This will be used in URLs later.
  • overview: This is a TextField column to include an overview of the course.
  • created: The date and time when the course was created. It will be automatically set by Django when creating new objects because of auto_now_add=True.

Each course is divided into several modules. Therefore, the Module model contains a ForeignKey field that points to the Course model.

Open the shell and run the following command to create the initial migration for this app:

python manage.py makemigrations

You will see the following output:

Migrations for 'courses':
0001_initial.py:
- Create model Course
- Create model Module
- Create model Subject
- Add field subject to course

Then, run the following command to apply all migrations to the database:

python manage.py migrate

You should see output including all applied migrations, including those of Django. The output will contain the following line:

Applying courses.0001_initial... OK

The models of our courses app have been synced to the database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset